如何在tomcat配置mysql数据连接池
发布时间:2025-05-20 01:36:57 发布人:远客网络
一、如何在tomcat配置mysql数据连接池
eb开发中与数据库的连接是必不可少的,而数据库连接池技术很好的优化了动态页与数据库的连接,相比单个连接数据库连接池节省了很大的资源。用一个通俗的比喻:如果一个人洗澡需花一桶水,那一百个人就要花一百桶水,太浪费了.如果都在池子里洗,洗多少个人都不怕了。
1.将MySQL的JDBC驱动复制到Tomcat安装目录里的lib文件夹下。驱动可以从MySQL官网上下载,为jar包。
2.将Tomcat的配置文件Context.xml做如下修改:
<Context path="/DBTest" docBase="DBTest"
debug="5" reloadable="true" crossContext="true">
<!-- maxActive: Maximum number of dB 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 dB connections to retain in pool.
Set to-1 for no limit. See also the DBCP documentation on this
and the minEvictableIdleTimeMillis configuration parameter.
<!-- maxWait: Maximum time to wait for a dB 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 dB username and password for dB 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 dB.
The autoReconnect=true argument to the url makes sure that the
mm.mysql JDBC Driver will automatically reconnect if mysqld closed the
connection. mysqld by default closes idle connections after 8 hours.
<Resource name="jdbc/TestDB" auth="Container" type="javax.sql.DataSource"
maxActive="100" maxIdle="30" maxWait="10000"
username="javauser" password="javadude" driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/javatest?autoReconnect=true"/>
注意代码中红色部分:DBTest改为自己的项目路径;TestDB改为自己的数据源名,但是后面使用时候要与这里的配置保持一致;javauser和 javauser改为自己MySQL的用户名密码;url的格式依次为jdbc:mysql://{你的数据库服务所在的IP,如果为本机就为localhost}:{你的数据库服务端口号}/{MySQL中要使用的数据库名称}?autoReconnect=true。
3.修改项目WEB-INF/web.xml配置文件(若无,请新建),在“</web-app>”之上添加如下代码:
<description>DB Connection</description>
<res-ref-name>jdbc/TestDB</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
上步中若修改了数据源名此步中红色部分请保持与上步中的一致。
Context initContext= new InitialContext();
Context envContext=(Context)initContext.lookup("java:/comp/env");
DataSource ds=(DataSource)envContext.lookup("jdbc/TestDB");
Connection conn= ds.getConnection();
注意红色部分与上两步中的一致;yoursql处写你的sql代码。
通过1-3步就在Tomcat中配置好了MySQL的数据库连接池。
二、Tomcat5.0.28配置mysql的连接池
1.在网上很多的文章都介绍在Tomcat/conf文件下的context.xml文件中添加如下的代码:
//这是为你的连接池起一个名字,后边在代码中会用到
//这个是你的mysql数据库的用户名和密码
driverClassName="com.mysql.jdbc.Driver"
//conn这个是你mysql中的数据库名
url="jdbc:mysql://localhost:3306/conn"
其实不用在conf下修改context.xml文件。
直接在自己的项目下的Webcontent/META-INF文件夹下新建一个context.xml文件将上面的代码拷贝到这个xml文件中就可以了。
(这里要注意的一点就是要将这个context.xml放在Webcontent/META-INF文件夹下,而不是放在Webcontent/WEB-INF文件下)
2.将下面代码拷贝到项目文件/Webcontent/WEB-INF文件夹下的web.xml下。注意要放在/web-app之前。
// DB Connections这是随意起的名字,没有影响
descriptionDB Connections/description
//jdbc/mysqlds这个就是你在context.xml中设置的连接池名字
res-ref-namejdbc/mysqlds/res-ref-name
res-typejavax.sql.DataSource/res-type
3.将mysql-connector-java-5.1.6-bin.jar驱动程序拷贝到项目文件/Webcontent/WEB-INF/lib文件夹下面,同时也要放在Tomcat/lib文件夹下面。
这样就完成了通过连接池的方式连接数据库了。
import java.sql.PreparedStatement;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ConnPslt extends HttpServlet{
private static final long serialVersionUID= 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
request.setCharacterEncoding("utf-8");
String title=request.getParameter("title");
String content=request.getParameter("content");
Context context=new InitialContext();
ds=(DataSource) context.lookup("java:/comp/env/jdbc/mysqlds");
Connection comm=ds.getConnection();
String sql="insert into webblog(title,content)values(?,?)";
PreparedStatement pstmt=comm.prepareStatement(sql);
// TODO Auto-generated catch block
%@ page language="java" contentType="text/html; charset=UTF-8"
!DOCTYPE html PUBLIC-//W3C//DTD HTML 4.01 Transitional//EN;
meta http-equiv="Content-Type" content="text/html; charset=UTF-8"
form name="form1" method="post" action="/DBconn/ConnPslt"
table width="320" border="1" align="center"
td colspan="2" align="center"留言板/td
input type="text" name="title" width="200"
textarea name="content" cols="30" rows="10"/textarea
td colspan="2" align="center"label
input type="submit" name="Submit" value="提交"
在控制台输出的结果如果是1,则添加成功,如果是0则说明数据库连接失败,要自己找找问题,
三、tomcat8.0怎么与mysql数据库连接池连接
到MySQL的网站上去下载MySQL的JDBC连接驱动mysql-connector-java-3.1.11-bin.jar,放到Tomcat的\common\lib目录下。
Tomcat的JDBC下载地址:,这是MySQL的3.1.13版本的驱动,它支持4.0以上版本的MySQL数据库,我下的是一个ZIP包,没有下那个GZ包版的,有7.9M大,把包中的mysql-connector-java-3.1.11-bin.jar文件解压出来放在Tomcat的安装目录的\common\lib下面就可以拉(我的目录:C:\Program Files\Apache Software Foundation\Tomcat 5.0\common\lib),因为tomcat运行时会先到这个目录下找Jar文件,如果你有兴趣的可以研究下它的源代码:P。还要查检下%TOMCAT_HOME%下面是否有commons-dbcp-*jar;commons-pool-*jar;commons-collections-*.jar存在,没有的话就要自己到去下,Tomcat5.0都自己带了有这几个commons文件。