当前位置:首页 » 操作系统 » 配置mysql数据库连接池

配置mysql数据库连接池

发布时间: 2025-09-23 20:29:49

① Tomcat5.0.28配置mysql的连接池

1.在网上很多的文章都介绍在Tomcat/conf文件下的context.xml文件中添加如下的代码:
Resource
//这是为你的连接池起一个名字,后边在代码中会用到
name="jdbc/mysqlds"
auth="Container"
type="javax.sql.DataSource"
maxActive="100"
maxIdel="30"
maxWait="10000"
//这个是你的mysql数据库的用户名和密码
username="root"
password="*******"
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之前。
resource-ref
// DB Connections 这是随意起的名字,没有影响
descriptionDB Connections/description
//jdbc/mysqlds这个就是你在context.xml中设置的连接池名字
res-ref-namejdbc/mysqlds/res-ref-name
res-typejavax.sql.DataSource/res-type
res-authContainer/res-auth
/resource-ref
3.将mysql-connector-java-5.1.6-bin.jar驱动程序拷贝到项目文件/Webcontent/WEB-INF/lib文件夹下面,同时也要放在Tomcat/lib文件夹下面。
这样就完成了通过连接池的方式连接数据库了。
可以通过以下代码就行测试:
新建一个servlet文件
Conntslt.java
package com.dbconn;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import javax.naming.Context;
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;
import javax.sql.DataSource;
public class ConnPslt extends HttpServlet {
private static final long serialVersionUID = 1L;

public ConnPslt() {
super();

}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request,response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
String title=request.getParameter("title");
String content=request.getParameter("content");
System.out.println(title);
DataSource ds = null;
try {
Context context=new InitialContext();
ds=(DataSource) context.lookup("java:/comp/env/jdbc/mysqlds");
} catch (NamingException e) {
System.out.println(e);
}
try {
Connection comm=ds.getConnection();
String sql="insert into webblog (title,content)values(?,?)";
PreparedStatement pstmt=comm.prepareStatement(sql);
pstmt.setString(1, title);
pstmt.setString(2, content);
int rs=pstmt.executeUpdate();
System.out.println(rs);

} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
}
再建一个jsp文件
index.jsp
%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%
!DOCTYPE html PUBLIC -//W3C//DTD HTML 4.01 Transitional//EN ;
html
head
meta http-equiv="Content-Type" content="text/html; charset=UTF-8"
titleInsert title here/title
/head
body
form name="form1" method="post" action="/DBconn/ConnPslt"
table width="320" border="1" align="center"
tr
td colspan="2" align="center"留言板/td
/tr
tr
td width="84"标题/td
td width="220"label
input type="text" name="title" width="200"
/label/td
/tr
tr
td height="73"内容/td
tdlabel
textarea name="content" cols="30" rows="10"/textarea
/label/td
/tr
tr
td colspan="2" align="center"label
input type="submit" name="Submit" value="提交"
/label/td
/tr
/table
/form
/body
/html
在控制台输出的结果如果是1,则添加成功,如果是0则说明数据库连接失败,要自己找找问题,

② Java数据库连接池的几种配置方法(以MySQL数

连接先建立一些连接,并且这些连接允许共享,因此这样就节省了每次连接的时间开销。Mysql数据库为例,连接池在Tomcat中的配置与使用。
1、创建数据库Student,表student
2、配置server.xml文件。Tomcat安装目录下conf中server.xml文件。
<GlobalNamingResources>
<Resource
name="jdbc/DBPool"
type="javax.sql.DataSource"
password=""
driverClassName="com.mysql.jdbc.Driver"
maxIdle="2"
maxWait="5000"
username="root"
url="jdbc:mysql://localhost:3306/student"
maxActive="3"
/>
</GlobalNamingResources>
name:指定连接池的名称
type:指定连接池的类,他负责连接池的事务处理
url:指定要连接的数据库
driverClassName:指定连接数据库使用的驱动程序
username:数据库用户名
password:数据库密码
maxWait:指定最大建立连接等待时间,如果超过此时间将接到异常
maxIdle:指定连接池中连接的最大空闲数
maxActive:指定连接池最大连接数
3、配置web.xml文件。
<web-app>
<resource-ref>
<description>mysql数据库连接池配置</description>
<res-ref-name>jdbc/DBPool</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
<res-sharing-scope>Shareable</res-sharing-scope>
</resource-ref>
</web-app>
4、配置context.xml文件
与server.xml文件所在的位置相同。
<Context>
<ResourceLink
name="jdbc/DBPool"
type="javax.sql.DataSource"
global="jdbc/DBPool"
/>
</Context>
5、测试
DataSource pool = null;
Context env = null;
Connection conn = null;
Statement st = null;
ResultSet rs = null;
try{
env = (Context)new InitialContext().lookup("java:comp/env");
//检索指定的对象,返回此上下文的一个新实例
pool = (DataSource)env.lookup("jdbc/DBPool");
//获得数据库连接池
if(pool==null){out.printl("找不到指定的连接池!");}
con = pool.getConnection();
st = con.createStatement();
rs = st.executeQuery("select * from student");
}catch(Exception ex){out.printl(ne.toString());}

③ 如何在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="javade" driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/javatest?autoReconnect=true"/>

</Context>
注意代码中红色部分:DBTest 改为自己的项目路径;TestDB改为自己的数据源名,但是后面使用时候要与这里的配置保持一致;javauser和 javauser改为自己MySQL的用户名密码;url的格式依次为jdbc:mysql://{你的数据库服务所在的IP,如果为本机就为localhost}:{你的数据库服务端口号}/{MySQL中要使用的数据库名称}?autoReconnect=true 。
3.修改项目WEB-INF/web.xml 配置文件(若无,请新建),在“</web-app>”之上添加如下代码:
<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>
上步中若修改了数据源名此步中红色部分请保持与上步中的一致。
4.代码示例:
Context initContext = new InitialContext();
Context envContext = (Context)initContext.lookup("java:/comp/env");
DataSource ds = (DataSource)envContext.lookup("jdbc/TestDB");
Connection conn = ds.getConnection();
Statement st = null;
ResultSet rs = null;
st = conn.createStatement();
rs = st.executeQuery(yoursql);
注意红色部分与上两步中的一致;yoursql处写你的sql代码。
通过1-3步就在Tomcat中配置好了MySQL的数据库连接池。

热点内容
倚天脚本 发布:2025-09-23 22:13:35 浏览:8
超级访问贾乃亮李小璐 发布:2025-09-23 22:11:24 浏览:810
编程拍摄 发布:2025-09-23 21:54:18 浏览:950
安卓怎么横屏发信息 发布:2025-09-23 21:52:49 浏览:199
欧几里德算法c语言 发布:2025-09-23 21:37:55 浏览:805
ssl证书https加密 发布:2025-09-23 21:37:02 浏览:273
java编译混淆 发布:2025-09-23 21:27:42 浏览:910
php当前页面跳转 发布:2025-09-23 21:27:35 浏览:862
ftp断开后挥几次手 发布:2025-09-23 21:15:19 浏览:654
android源码淘宝 发布:2025-09-23 21:10:30 浏览:38