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

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