1.把驱动拷贝到%TOMCAT_HOME%/common/lib目录下
2.新建数据库表,并向表中添加记录
1None.gif use test;
2None.gif create table testdata (
3None.gif        id int not null auto_increment primary key,
4None.gif        foo varchar(25), 
5None.gif        bar int);
6None.gifinsert into testdata values(null'hello'12345);
7None.gif

3.在%TOMCAT_HOME%/config/server.xml文件中加入如下一段配置信息(在</Host>之前)
 
 1None.gif        <!--数据库连接池配置-->
 2None.gif    <!--path:Your webApp directory-->
 3None.gif<Context path="/WS4Motel" docBase="WS4Motel"
 4None.gif        debug="5" reloadable="true" crossContext="true">
 5None.gif
 6None.gif    <!-- maxActive: Maximum number of dB connections in pool. Make sure you
 7None.gif         configure your mysqld max_connections large enough to handle
 8None.gif         all of your db connections. Set to 0 for no limit.
 9None.gif         -->
10None.gif
11None.gif    <!-- maxIdle: Maximum number of idle dB connections to retain in pool.
12None.gif         Set to -1 for no limit.  See also the DBCP documentation on this
13None.gif         and the minEvictableIdleTimeMillis configuration parameter.
14None.gif         -->
15None.gif
16None.gif    <!-- maxWait: Maximum time to wait for a dB connection to become available
17None.gif         in ms, in this example 10 seconds. An Exception is thrown if
18None.gif         this timeout is exceeded.  Set to -1 to wait indefinitely.
19None.gif         -->
20None.gif
21None.gif    <!-- username and password: MySQL dB username and password for dB connections  -->
22None.gif
23None.gif    <!-- driverClassName: Class name for the old mm.mysql JDBC driver is
24None.gif         org.gjt.mm.mysql.Driver - we recommend using Connector/J though.
25None.gif         Class name for the official MySQL Connector/J driver is com.mysql.jdbc.Driver.
26None.gif         -->
27None.gif    
28None.gif    <!-- url: The JDBC connection url for connecting to your MySQL dB.
29None.gif         The autoReconnect=true argument to the url makes sure that the
30None.gif         mm.mysql JDBC Driver will automatically reconnect if mysqld closed the
31None.gif         connection.  mysqld by default closes idle connections after 8 hours.
32None.gif         -->
33None.gif
34None.gif  <Resource name="jdbc/WS4Motel" auth="Container" type="javax.sql.DataSource"
35None.gif               maxActive="100" maxIdle="30" maxWait="10000"
36None.gif               username="root" password="83072674" driverClassName="com.mysql.jdbc.Driver"
37None.gif               url="jdbc:mysql://localhost:3306/test?autoReconnect=true"/>
38None.gif
39None.gif</Context>
40None.gif        <!--数据库连接池配置-->
41None.gif
 4.写个简单程序测试下:
1None.gif<%@ taglib uri="[url]http://java.sun.com/jsp/jstl/sql[/url]" prefix="sql" %>
 2None.gif<%@ taglib uri="[url]http://java.sun.com/jsp/jstl/core[/url]" prefix="c" %>
 3None.gif
 4None.gif<sql:query var="rs" dataSource="jdbc/TestDB">
 5None.gifselect id, foo, bar from testdata
 6None.gif</sql:query>
 7None.gif
 8None.gif<html>
 9None.gif  <head>
10None.gif    <title>DB Test</title>
11None.gif  </head>
12None.gif  <body>
13None.gif
14None.gif  <h2>Results</h2>
15None.gif  
16None.gif<c:forEach var="row" items="${rs.rows}">
17ExpandedBlockStart.gifContractedBlock.gif    Foo $dot.gif{row.foo}<br/>
18ExpandedBlockStart.gifContractedBlock.gif    Bar $dot.gif{row.bar}<br/>
19None.gif</c:forEach>
20None.gif
21None.gif  </body>
22None.gif</html>
23None.gif