---
title: "tomcat连接池的配置与使用数据库mysql"
author: "fuyangchang"
date: "2007-06-11T17:32:00Z"
tags: []
slug: "tomcat-mysql"
draft: false
lastmod: "2007-06-11T17:32:00Z"
id: 104081
url: "https://csdn.iteye.com/blog/104081"
---

## tomcat连接池的配置与使用数据库mysql

                                      

关于tomcat连接池的配置，我在自己开始的时候碰到过许多的问题，至少失败过六次，今天终于成功了。对于这个问题的产生与由来我也给大伙说说。  
在《程序员csdn开发高手》这本杂志的2004年第02期，一位名叫宋廷宇的老师写了一篇文章，名赤《tomcat连接池的图形化配置与使用》，我看到 了这篇文章，该作者所使用的数据库是sql server，而我所使用的数据库是mysql，tomcat版本为5.0　19。文中有这么一段话：“在tomcat 4.1.18及后续版本中提供了图形界面的web应用发布工具，但是使用这个工具发布采用数据库连接池的web应用时，自动生成的配置不能找到数据源。这 是一个bug，相信在后续的版本中所有关于数据库连接池的配置和发布都不需要在手工修改配置文件，而使用简单方便的全图形化工具”。当时我个人认为：我所 使用的tomcat 5.0版的，bug应该修正了吧，况且数据库配置嘛，换汤不换药，原理应该是一样的，但是我严格按照这篇文章上来配置，结果事与愿为，配置成功运行后出错 了。附错误如下：  
org.apache.commons.dbcp.SQLNestedException: Cannot create JDBC driver of class '  
' for connect URL 'null', cause: No suitable driver  
相信学过jsp连接数据库的人都有常识，那就是将驱动加入CLASSPATH。排除错误，驱动没问题。迷惑中  
于是乎，检查，重来再检查，再重来，错误不变。  
既然图形下配置错误，那么我手动配置。上网搜索，最权威的当然就是tomcat的doc。刚好文档中有一篇讲如何配置mysql数据库连接池的（全英文 的，幸好我英语过得去），一步一步严格按照它的格式来配置，当我看到同样的错误时，犹如晴天劈雳。一个字：晕！！！差点放弃。（如果放弃了，就不会有我今 天这篇文章了，好险呀！！！）  
因此我认为tomcat有问题。这个错误很多人都见过，在csdn及本论坛上，有许多人问到这样的问题，为这问题，我还在国外的论坛上搜索了，也有人问到 这样的问题，他们说这是tomcat的一个bug,于是我就默认了。后来想想不对，如果是bug的话，jarkata项目组织肯定会贴出来并且修正这个 bug的。  
我就下载了最新版本的tomcat(5.0 25)再次试验，首先是按照书上的文章进行图形化配置，结果依然，心里想，bug肯定修正，且我与作者的步骤一样。问题休在？？？沉着冷静下来，再次看看 tomcat的文档。又回想起宋廷宇老师的文章中那段话，莫非真的是图形配置中的bug。  
[http://jakarta.apache.org/tomcat/tomcat-5.0-doc/jndi-datasource-examples-howto.html](http://jakarta.apache.org/tomcat/tomcat-5.0-doc/jndi-datasource-examples-howto.html)  
好，就来手工配置吧。于是又打开tomcat的doc开始了手工配置。配置之前建议大家对server.xml文件做一个备份，原因就不讲了。  
打开server.xml文件，在哪？？晕！！打开tomcat$\\conf文件夹（tomcat$指的是tomcat的安装目录，下皆同）。至于数据库的建立我就不说了，自己按照文章去做吧，说明一下，尽管我不说，我还是按照文章上来做的，你们照葫芦画瓢即可。  
首先将那段

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;Context path="/DBTest" docBase="DBTest"  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)       debug="5" reloadable="true" crossContext="true"&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;!—注意到这里的DBTest了吧，这就是要求大家建立DBTest目录的原因。--&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) &lt;Logger className="org.apache.catalina.logger.FileLogger"  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)            prefix="localhost\_DBTest\_log." suffix=".txt"  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)            timestamp="true"/&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) &lt;Resource name="jdbc/TestDB"  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)              auth="Container"  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)              type="javax.sql.DataSource"/&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) &lt;ResourceParams name="jdbc/TestDB"&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)   &lt;parameter&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)     &lt;name&gt;factory&lt;/name&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)     &lt;value&gt;org.apache.commons.dbcp.BasicDataSourceFactory&lt;/value&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)   &lt;/parameter&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)   &lt;!-- Maximum number of dB connections in pool. Make sure you  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)        configure your mysqld max\_connections large enough to handle  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)        all of your db connections. Set to 0 for no limit.  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)        --&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)   &lt;parameter&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)     &lt;name&gt;maxActive&lt;/name&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)     &lt;value&gt;100&lt;/value&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)   &lt;/parameter&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)   &lt;!-- Maximum number of idle dB connections to retain in pool.  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)        Set to 0 for no limit.  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)        --&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)   &lt;parameter&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)     &lt;name&gt;maxIdle&lt;/name&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)     &lt;value&gt;30&lt;/value&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)   &lt;/parameter&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)   &lt;!-- Maximum time to wait for a dB connection to become available  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)        in ms, in this example 10 seconds. An Exception is thrown if  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)        this timeout is exceeded.  Set to -1 to wait indefinitely.  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)        --&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)   &lt;parameter&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)     &lt;name&gt;maxWait&lt;/name&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)     &lt;value&gt;10000&lt;/value&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)   &lt;/parameter&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)   &lt;!-- MySQL dB username and password for dB connections  --&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)   &lt;parameter&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    &lt;name&gt;username&lt;/name&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    &lt;value&gt;javauser&lt;/value&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;!—数据库用户名--&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)   &lt;/parameter&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)   &lt;parameter&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    &lt;name&gt;password&lt;/name&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    &lt;value&gt;javadude&lt;/value&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;!—数据库密码--&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)   &lt;/parameter&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)   &lt;!-- Class name for the old mm.mysql JDBC driver - uncomment this entry and comment next  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)        if you want to use this driver - we recommend using Connector/J though  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)   &lt;parameter&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)      &lt;name&gt;driverClassName&lt;/name&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)      &lt;value&gt;org.gjt.mm.mysql.Driver&lt;/value&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;/parameter&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)这里面是被注释的，因为以前连接mysql是这样连接的，现在建议用下面的方法，自己可以看到。  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)    --&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)     
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)   &lt;!-- Class name for the official MySQL Connector/J driver --&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)   &lt;parameter&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)      &lt;name&gt;driverClassName&lt;/name&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)      &lt;value&gt;com.mysql.jdbc.Driver&lt;/value&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)   &lt;/parameter&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)     
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)   &lt;!-- The JDBC connection url for connecting to your MySQL dB.  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)        The autoReconnect=true argument to the url makes sure that the  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)        mm.mysql JDBC Driver will automatically reconnect if mysqld closed the  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)        connection.  mysqld by default closes idle connections after 8 hours.  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)        --&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)   &lt;parameter&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)     &lt;name&gt;url&lt;/name&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)     &lt;value&gt;jdbc:mysql://localhost:3306/javatest?autoReconnect=true&lt;/value&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;!--这是数据库的地址，也可以不要这么繁琐，改为  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)jdbc:mysql://localhost/javatest即可,javatest为数据库名--&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)   &lt;/parameter&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) &lt;/ResourceParams&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;/Context&gt;

贴在这了，别嫌长啊。我加了注释。至于我们贴到什么位置，在&lt;/Host&gt;前面，找到相对应的位置，由于我的server.xml文件未备份，所以我无法再次将所贴位置告诉大家了，不好意思，有事给我发过来，我一定还记得的，到时再手把手教你们。  
在tomcat $\\webapps文件夹下建立一个名为DBTest的文件夹，注意大小写。在此文件夹下建立WEB-INF文件夹，及一个文件:test.jsp，再在 WEB-INF文件夹下建立两个文件夹：lib及classes，将数据库驱动文件拷贝到lib文件夹下，同样还在WEB-INF文件夹下建一个文件 web.xml，看到doc上有现成的，要不要拷贝呀，这就有个问题，doc有个问题，有空给它们写封信吧，这也是我经过实现才发现的。  
&lt;?xml version="1.0" encoding="ISO-8859-1"?&gt;  
&lt;web-app xmlns="[http://java.sun.com/xml/ns/j2ee"](http://java.sun.com/xml/ns/j2ee%22)  
   xmlns:xsi="[http://www.w3.org/2001/XMLSchema-instance"](http://www.w3.org/2001/XMLSchema-instance%22)  
   xsi:schemaLocation="[http://java.sun.com/xml/ns/j2ee](http://java.sun.com/xml/ns/j2ee)  
[http://java.sun.com/xml/ns/j2ee/web-app\_2\_4.xsd"](http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd%22)  version="2.4"&gt;  
 &lt;description&gt;MySQL Test App&lt;/description&gt;  
 &lt;resource-ref&gt;  
     &lt;description&gt;DB Connection&lt;/description&gt;  
     &lt;res-ref-name&gt;jdbc/TestDB&lt;/res-ref-name&gt;  
     &lt;res-type&gt;javax.sql.DataSource&lt;/res-type&gt;  
     &lt;res-auth&gt;Container&lt;/res-auth&gt;  
 &lt;/resource-ref&gt;  
&lt;/web-app&gt;  
用我的，没错的。  
Test.jsp

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)&lt;%...@ page contentType="text/html; charset=GBK"%&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)&lt;%...@ page import="java.sql.\*,javax.sql.DataSource,javax.naming.\*"%&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;html&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;head&gt;&lt;title&gt;test.jsp&lt;/title&gt;&lt;/head&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;body bgcolor="#ffffff"&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;h1&gt;test Tomcat&lt;/h1&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)&lt;%...  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)try  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif){  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)Context initCtx=new InitialContext();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)DataSource ds = (DataSource)initCtx.lookup("java:comp/env/jdbc/TestDB");  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)Connection conn=ds.getConnection();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)out.println("data from database:&lt;br&gt;");  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)Statement stmt=conn.createStatement();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)ResultSet rs =stmt.executeQuery("select id, foo, bar from testdata");  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)while(rs.next())  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif){  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)out.println(rs.getInt("id"));  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)out.println(rs.getString("foo"));  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)out.println(rs.getString("bar"));  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)rs.close();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)stmt.close();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)catch(Exception e)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif){  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)e.printStackTrace();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)%&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;/body&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;/html&gt;

doc上的test.jsp是使用了tag ，我认为没必要，大家反而看不懂，并且它没有关闭connection及statement，个人认为存在不安全因素，因此我自己也改过来了。  
接下来的事情嘛，自己测试，测试前要将mysql打开，常识性的错误别犯了。  
启动tomcat  
至http://localhost:8080/DBTest/test.jsp  
在我的界面上是  
test Tomcat  
data from database:  
1 hello 12345  
大家尽情享用吧。