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

tomcat数据库连接池配置

发布时间: 2025-06-11 19:26:40

⑴ 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中建立数据库连接池,有哪几个步骤

Context context=new InitialContext();
DataSource ds=(DataSource)context.lookup("java:comp/env/jdbc/"+projectName);这里的projectName指的是你的项目名称然后在tomcatde 的\conf\Catalina\localhost目录下配置一个和你项目名称相同的xml文件文件的配置方法可以去网络下很多的,这样就可以使用连接池了。

⑶ 如何在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的数据库连接池。

⑷ 如何实现Tomcat连接池数据库密码加密

问题解决思路:
将配置文件用户相关的信息(例如:密码)进行加密使其以密文形式存在,进行初始化连接池的时候进行解密操作,达到成功创建连接池的目的。Tomcat默认使用DBCP连接池(基于common-pool的一种连接池实现),对org.apache.commons.dbcp.BasicDataSourceFactory类修改,把数据库密码字段(加密后的密文)用解密程序解密,获得解密后的明文即可。
具体实现:
1. 修改org.apache.commons.dbcp.BasicDataSourceFactory类文件
找到数据源密码设置部分
value = properties.getProperty(PROP_PASSWORD);
if (value != null) {
dataSource.setPassword(value);
}
修改为:
value = properties.getProperty(PROP_PASSWORD);
if (value != null) {
dataSource.setPassword(Encode.decode(value));
}
将配置文件中的“密码”(加密后的结果)取出,调用加解密类中的解密方法Encode.decode(value)进行解密。
2. 加密类Encode.java,本例中使用加密解密模块比较简单只是用来说明问题,密文为明文的十六进制串。
public class Encode {
//编码-普通字符串转为十六进制字符串
public static String encode(String password){
String result = “”;
byte[] psd = password.getBytes();
for(int i=0;i<psd.length;i++){
result += Integer.toHexString(psd[i]&0xff);
}
return result;
}
//解码–十六进制字符串转为普通字符串
public static String decode(String password){
String result = “”;
password = password.toUpperCase();
int length = password.length() / 2;
char[] hexChars = password.toCharArray();
byte[] d = new byte[length];
for (int i = 0; i < length; i++) {
int pos = i * 2;
d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));
}
result = new String(d);
return result;
}
//字符转字节
public static byte charToByte(char c) {
return (byte) “0123456789ABCDEF”.indexOf(c);
}
}
3. 数据库连接池文件,红色字体为数据源配置中密码设置,此时已经改为密文形式。
<?xml version=’1.0′ encoding=’utf-8′?>
<Context docBase=”reportmis” path=”/reportmis” privileged=”true” workDir=”work\Catalina\localhost\reportmis”>
<Resource auth=”Container” name=”mis2datasource” type=”javax.sql.DataSource”/>
<ResourceParams name=”mis2datasource”>
<parameter>
<name>password</name>
<value>696e65743231</value>
</parameter>
<parameter>
<name>url</name>
<value>jdbc:oracle:thin:@127.0.0.1:1521:orcl</value>
</parameter>
<parameter>
<name>driverClassName</name>
<value>oracle.jdbc.driver.OracleDriver</value>
</parameter>
<parameter>
<name>username</name>
<value>wanfang</value>
</parameter>
</ResourceParams>
</Context>
4. 将修改后的BasicDataSourceFactory.java和新添加的Encode.java编译后的class类文件重新打包进commons-dbcp-1.4.jar,将该包拷贝进tomcat下的common/lib目录中,重启tomcat。此时tomcat下部署的应用在连接数据源的时候都可以在不暴露密码明文的情况下进行连接。

⑸ tomcat如何配置数据库连接池,使得连接中断后自动重连

给你一段代码,看看对你有没有帮助:
这是我的tomcatde DHCP的配置
<Resource driverClassName="com.microsoft.sqlserver.jdbc.SQLServerDriver" logAbandoned="true" maxActive="20" maxIdle="2" maxWait="5000" name="system" password="sa" removeAbandoned="true" removeAbandonedTimeout="60" type="javax.sql.DataSource"
url="jdbc:sqlserver://127.0.0.1:1433;DatabaseName=base" username="sa"/>
当中的
logAbandoned="true" removeAbandoned="true" removeAbandonedTimeout="60"就是用来配置数据库断开后自动连接的。

热点内容
安卓和app运行哪个快 发布:2025-06-13 08:54:50 浏览:730
启动磁盘加密 发布:2025-06-13 08:45:28 浏览:722
谷歌怎么在安卓 发布:2025-06-13 08:37:13 浏览:796
linux内核重启之后要编译吗 发布:2025-06-13 08:27:52 浏览:210
手机存储器已几乎满 发布:2025-06-13 08:27:44 浏览:545
怎么开启服务器的ftp 发布:2025-06-13 08:05:25 浏览:645
js无需编译 发布:2025-06-13 07:36:35 浏览:805
linux共享上网 发布:2025-06-13 07:32:53 浏览:533
查询域主机服务器ip 发布:2025-06-13 07:20:13 浏览:122
通过ip进服务器文件看不到了 发布:2025-06-13 07:07:46 浏览:584