本篇文章展示了Java用連接池連接數(shù)據(jù)庫的方法具體操作,代碼簡明扼要容易理解,絕對能讓你眼前一亮,通過這篇文章的詳細(xì)介紹希望你能有所收獲。

創(chuàng)新互聯(lián)公司是一家專業(yè)提供昆山企業(yè)網(wǎng)站建設(shè),專注與成都網(wǎng)站設(shè)計、網(wǎng)站建設(shè)、html5、小程序制作等業(yè)務(wù)。10年已為昆山眾多企業(yè)、政府機(jī)構(gòu)等服務(wù)。創(chuàng)新互聯(lián)專業(yè)的建站公司優(yōu)惠進(jìn)行中。
傳統(tǒng)方式和連接池方式
傳統(tǒng)方式的步驟
使用傳統(tǒng)方式在Java中使用JDBC連接數(shù)據(jù)庫,完成一次數(shù)據(jù)庫的操作,一般有以下幾個步驟:
1. 加載驅(qū)動。
2. 建立連接。
3. 執(zhí)行SQL語句。
4. 釋放連接。
5. 傳統(tǒng)方式的弊端
每一次對數(shù)據(jù)庫的操作都要建立一次連接,并且會將得到的Connection對象加載到內(nèi)存中,導(dǎo)致消耗了大量的內(nèi)存和時間。如果短時間有很多需要進(jìn)行建立連接的操作,會導(dǎo)致占用很多系統(tǒng)資源,甚至?xí)?dǎo)致服務(wù)器崩潰。
同建立連接相對應(yīng),每次使用都需要手動釋放連接,如果忘記釋放連接或者程序出現(xiàn)異常未能成功釋放,會導(dǎo)致內(nèi)存泄露。
此外,傳統(tǒng)方式并不能控制連接的數(shù)量,如果連接的人數(shù)過多,會導(dǎo)致無限制的創(chuàng)建連接對象,導(dǎo)致內(nèi)存開銷過大,服務(wù)器崩潰。
連接池的步驟
1. 創(chuàng)建連接池并配置連接屬性。
2. 使用連接池獲取連接。
連接池的優(yōu)勢
每次需要連接數(shù)據(jù)庫時,不需要建立連接,而是通過連接池獲取,由連接池提供連接。
在使用完連接后,不需要手動釋放連接,而是交由連接池釋放連接。
可以通過連接池控制連接的數(shù)量,在連接池里的連接可多次重復(fù)使用,避免了無限制創(chuàng)建連接的問題。
使用連接池
使用C3P0數(shù)據(jù)庫連接池
導(dǎo)入jar包:
c3p0-0.9.5.2.jar
在當(dāng)前項(xiàng)目的代碼根目錄 src 下新建名為 c3p0-config.xml 的配置文件,注意文件名稱不可改變,內(nèi)容如下:
<c3p0-config>
<!-- 連接名稱 -->
<named-config name="MySQL">
<!-- 接數(shù)據(jù)庫的驅(qū)動類名 -->
<property name="driverClass">com.mysql.jdbc.Driver</property>
<!-- 連接屬性 -->
<property name="jdbcUrl">jdbc:mysql://192.168.35.128:3306/demo</property>
<property name="user">root</property>
<property name="password">123456</property>
<!-- 當(dāng)連接池用完時等待獲取新連接的時間,超時后將拋出SQLException,單位毫秒,如設(shè)為0則無限期等待。默認(rèn)為0。 -->
<property name="checkoutTimeout">5000</property>
<!-- 當(dāng)連接用盡后,一次獲取的連接個數(shù) -->
<property name="acquireIncrement">2</property>
<!-- 初始連接數(shù) -->
<property name="initialPoolSize">1</property>
<!-- 最小連接數(shù) -->
<property name="minPoolSize">3</property>
<!-- 最大連接數(shù) -->
<property name="maxPoolSize">5</property>
</named-config>
</c3p0-config>程序代碼:
public class TestDataPool {
// 根據(jù)配置文件里的名稱創(chuàng)建連接池
public static ComboPooledDataSource cpds = new ComboPooledDataSource("mysql");
/**
* 主程序
*/
public static void main(String[] args) {
// 模擬多次對數(shù)據(jù)庫的查詢操作
for (int i = 0; i < 6; i++) {
new Thread(new Runnable() {
@Override
public void run() {
select();
}
}, "線程" + i).start();
}
}
/**
* 查詢程序
*/
public static void select() {
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
// 獲取連接并執(zhí)行SQL
try {
conn = cpds.getConnection();
pstmt = conn.prepareStatement("select * from student where id = 906");
rs = pstmt.executeQuery();
while (rs.next()) {
System.out.println(Thread.currentThread().getName() + "\t" + rs.getString(1) + "\t" + rs.getString(2) + "\t" + rs.getString("address"));
}
} catch (Exception e) {
e.printStackTrace();
} finally {
// 釋放資源
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
try {
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}使用DBCP數(shù)據(jù)庫連接池
導(dǎo)入jar包:
commons-dbcp-1.4.jar2 commons-pool-1.5.5.jar
在當(dāng)前項(xiàng)目的代碼根目錄 src 下新建名為 dbcp.properties 的配置文件,文件名需要同代碼中引用的文件名一致,內(nèi)容如下:
# 接數(shù)據(jù)庫的驅(qū)動類名 driverClassName=com.mysql.jdbc.Driver # 連接屬性 url=jdbc:mysql://192.168.35.128:3306/demo username=root password=123456 # 初始化連接數(shù) initialSize=10 # 最大連接數(shù) maxActive=15
程序代碼:
public class TestDBCP {
// 根據(jù)配置文件里的名稱創(chuàng)建連接池
private static DataSource source = null;
static {
Properties pros = new Properties();
InputStream is = TestDBCP.class.getClassLoader().getResourceAsStream("dbcp.properties");
try {
pros.load(is);
source = BasicDataSourceFactory.createDataSource(pros);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 主程序
*/
public static void main(String[] args) {
// 模擬多次對數(shù)據(jù)庫的查詢操作
for (int i = 0; i < 6; i++) {
new Thread(new Runnable() {
@Override
public void run() {
select();
}
}, "線程" + i).start();
}
}
/**
* 查詢程序
*/
public static void select() {
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
// 獲取連接并執(zhí)行SQL
try {
conn = source.getConnection();
pstmt = conn.prepareStatement("select * from student where id = 906");
rs = pstmt.executeQuery();
while (rs.next()) {
System.out.println(Thread.currentThread().getName() + "\t" + rs.getString(1) + "\t" + rs.getString(2) + "\t" + rs.getString("address"));
}
} catch (Exception e) {
e.printStackTrace();
} finally {
// 釋放資源
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
try {
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}看完上述內(nèi)容,你們掌握J(rèn)ava用連接池連接數(shù)據(jù)庫的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!
文章標(biāo)題:Java用連接池連接數(shù)據(jù)庫的方法
網(wǎng)站地址:http://www.chinadenli.net/article38/igjgsp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供虛擬主機(jī)、用戶體驗(yàn)、網(wǎng)站策劃、網(wǎng)站維護(hù)、網(wǎng)頁設(shè)計公司、品牌網(wǎng)站制作
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)