當前位置:首頁 » 操作系統 » 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"就是用來配置資料庫斷開後自動連接的。

熱點內容
壓縮機一段 發布:2025-06-13 04:28:38 瀏覽:2
安卓怎麼查看obb 發布:2025-06-13 04:06:36 瀏覽:788
自己的手機賬號密碼在哪裡找 發布:2025-06-13 03:49:33 瀏覽:67
id伺服器如何填 發布:2025-06-13 03:36:32 瀏覽:388
為什麼很多安卓手機不敵蘋果 發布:2025-06-13 03:34:54 瀏覽:392
uc如何解鎖手機密碼 發布:2025-06-13 03:22:06 瀏覽:563
vs連接sql資料庫代碼 發布:2025-06-13 03:21:57 瀏覽:745
咋學編程 發布:2025-06-13 03:18:05 瀏覽:61
路虎攬勝運動最低配是哪個配置 發布:2025-06-13 02:50:23 瀏覽:236
phptimer 發布:2025-06-13 02:41:54 瀏覽:279