當前位置:首頁 » 操作系統 » 配置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 瀏覽:809
編程拍攝 發布: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