Tomcat教程

全部教程

×

MySQL DBCP 范例

MySQL DBCP 范例

 简介

已报告的能够正常运作的 MySQL 与 JDBC 驱动的版本号为:

  • MySQL 3.23.47、使用 InnoDB 的 MySQL 3.23.47、MySQL 3.23.58 以及 MySQL 4.0.1 alpha
  • Connector/J 3.0.11-stable (JDBC 官方驱动)
  • mm.mysql 2.0.14 (一个较老的 JDBC 第三方驱动)

在继续下一步的操作之前,千万不要忘了将 JDBC 驱动的 JAR 文件复制到 $CATALINA_HOME/lib 中。

MySQL 配置

一定要按照下面的说明去操作,否则会出现问题。

创建一个新的测试用户、一个新的数据库,以及一张新的测试表。必须为 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,查看你的第一个劳动成果了。