Technical otaku
2022-03-21
Introduction
The reported version numbers of the MySQL and JDBC drivers that work correctly are:
MySQL 3.23.47, MySQL 3.23.47 with InnoDB, MySQL 3.23.58, and MySQL 4.0.1 alpha
Connector/J 3.0.11-stable (JDBC official driver)
mm.mysql 2.0.14 (an older JDBC third-party driver)
Don't forget to copy the JDBC driver JAR file to $CATALINA_HOME/lib before proceeding to the next step.
MySQL configuration
Be sure to follow the instructions below, otherwise problems will occur.
Create a new test user, a new database, and a new test table. A password must be specified for the MySQL user. If the password is empty, then when connecting, it will not be able to drive normally.
mysql> GRANT ALL PRIVILEGES ON *.* TO [email protected] -> IDENTIFIED BY 'javadude' WITH GRANT OPTION;mysql> create database javatest;mysql> use javatest;mysql> create table testdata ( -> id int not null auto_increment primary key, -> foo varchar(25), -> bar int);
Note: Once the test is over, the user in the example above should be deleted!
Let's insert some test data into the testdata table:
mysql> insert into testdata values(null, 'hello', 12345);Query OK, 1 row affected (0.00 sec)
mysql> select * from testdata;+----+-------+-------+| ID | FOO | BAR |+----+-------+-------+| 1 | hello | 12345 |+----+-------+-------+1 row in set (0.00 sec)
mysql>
context configuration
Add a resource declaration in the Context to configure the JNDI data source in Tomcat.
Examples are as follows:
<Context>
<!-- maxTotal: Maximum number of database connections in pool. Make sure you
configure your mysqld max_connections large enough to handle
all of your db connections. Set to -1 for no limit.
-->
<!-- maxIdle: Maximum number of idle database connections to retain in pool.
Set to -1 for no limit. See also the DBCP documentation on this
and the minEvictableIdleTimeMillis configuration parameter.
-->
<!-- maxWaitMillis: Maximum time to wait for a database connection to become available
in ms, in this example 10 seconds. An Exception is thrown if
this timeout is exceeded. Set to -1 to wait indefinitely.
-->
<!-- username and password: MySQL username and password for database connections -->
<!-- driverClassName: Class name for the old mm.mysql JDBC driver is
org.gjt.mm.mysql.Driver - we recommend using Connector/J though.
Class name for the official MySQL Connector/J driver is com.mysql.jdbc.Driver.
-->
<!-- url: The JDBC connection url for connecting to your MySQL database.
-->
<Resource name="jdbc/TestDB" auth="Container" type="javax.sql.DataSource"
maxTotal="100" maxIdle="30" maxWaitMillis="10000"
username="javauser" password="javadude" driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/javatest"/></Context>
web.xml configuration
Create a WEB-INF/web.xml file for the test application:
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4">
<description>MySQL Test App</description>
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/TestDB</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref></web-app>
test code
Create a simple test.jsp page, which will be used later.
<%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql" %><%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %><sql:query var="rs" dataSource="jdbc/TestDB">select id, foo, bar from testdata</sql:query><html>
<head>
<title>DB Test</title>
</head>
<body>
<h2>Results</h2><c:forEach var="row" items="${rs.rows}">
Foo ${row.foo}<br/>
Bar ${row.bar}<br/></c:forEach>
</body></html>
JSP pages use JSTL's SQL and Core taglibs. You can get it from the Apache Tomcat Taglibs - Standard Tag Library project, but note that it should be version 1.1.x or later. After downloading JSTL, copy jstl.jar and standard.jar into the WEB-INF/lib directory of the web application.
Finally, deploy your application to $CATALINA_BASE/webapps in two ways: either deploy the application as a WAR file called DBTest.war; or place the application in a subdirectory called DBTest.
After the deployment is complete, you can enter http://localhost:8080/DBTest/test.jsp in the browser to view your first labor results.
0 Comments