已報告的能夠正常運作的 MySQL 與 JDBC 驅動的版本號為:
在繼續下一步的操作之前,千萬不要忘了將 JDBC 驅動的 JAR 文件復制到 $CATALINA_HOME/lib 中。
一定要按照下面的說明去操作,否則會出現問題。
創建一個新的測試用戶、一個新的數據庫,以及一張新的測試表。必須為 MySQL 用戶指定一個密碼。如果密碼為空,那么在連接時,就會無法正常驅動。
mysql> GRANT ALL PRIVILEGES ON *.* TO javauser@localhost
????-> ??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);
注意:一旦測試結束,就該把上例中的這個用戶刪除!
下面在 testdata 表中插入一些測試數據:
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 中添加資源聲明,以便在 Tomcat 中配置 JNDI 數據源。
范例如下:
<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 配置
為該測試應用創建一個 WEB-INF/web.xml 文件:
<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.jsp 頁面,稍后將用到它。
<%@ 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 頁面用到了 JSTL 的 SQL 和 Core taglibs。你可以從 Apache Tomcat Taglibs - Standard Tag Library 項目中獲取它,不過要注意應該是 1.1.x 或之后的版本。下載 JSTL 后,將 jstl.jar 和 standard.jar 復制到 Web 應用的 WEB-INF/lib 目錄中。
最后,將你的應用部署到 $CATALINA_BASE/webapps,可以采用兩種方式:或者將應用以名叫 DBTest.war 的 WAR 文件形式部署;或者把應用放入一個叫 DBTest 的子目錄中。
部署完畢后,就可以在瀏覽器輸入 http://localhost:8080/DBTest/test.jsp,查看你的第一個勞動成果了。