Java使用MySQL實(shí)現(xiàn)連接池代碼實(shí)例
官方:數(shù)據(jù)庫(kù)連接池(Connection pooling)是程序啟動(dòng)時(shí)建立足夠的數(shù)據(jù)庫(kù)連接,并將這些連接組成一個(gè)連接池,由程序動(dòng)態(tài)地對(duì)連接池中的連接進(jìn)行申請(qǐng),使用,釋放。
理解:創(chuàng)建數(shù)據(jù)庫(kù)連接池是一個(gè)很耗時(shí)的操作,也容易對(duì)數(shù)據(jù)庫(kù)造成安全隱患。所以,在程序初始化的時(shí)候,集中創(chuàng)建多個(gè)數(shù)據(jù)庫(kù)連接池,并把他們集中管理,供程序使用,可以保證較快的數(shù)據(jù)庫(kù)讀寫速度,還更加的安全可靠。
手動(dòng)配置連接池:
/**
* 手動(dòng)設(shè)置連接池
*/
public void demo1(){
// 獲得連接:
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try{
// 創(chuàng)建連接池:
ComboPooledDataSource dataSource = new ComboPooledDataSource();
// 設(shè)置連接池的參數(shù):
dataSource.setDriverClass("com.mysql.jdbc.Driver");
dataSource.setJdbcUrl("jdbc:mysql:///jdbctest");
dataSource.setUser("root");
dataSource.setPassword("abc");
dataSource.setMaxPoolSize(20);
dataSource.setInitialPoolSize(3);
// 獲得連接:
conn = dataSource.getConnection();
// 編寫Sql:
String sql = "select * from user";
// 預(yù)編譯SQL:
pstmt = conn.prepareStatement(sql);
// 設(shè)置參數(shù)
// 執(zhí)行SQL:
rs = pstmt.executeQuery();
while(rs.next()){
System.out.println(rs.getInt("uid")+" "+rs.getString("username")+" "+rs.getString("password")+" "+rs.getString("name"));
}
}catch(Exception e){
e.printStackTrace();
}finally{
JDBCUtils.release(rs, pstmt, conn);
}
}
使用配置文件配置連接池:
配置文件xml如下:
<?xml version="1.0" encoding="UTF-8"?> <c3p0-config> <default-config> <property name="driverClass">com.mysql.jdbc.Driver</property> <property name="jdbcUrl">jdbc:mysql:///jdbctest</property> <property name="user">root</property> <property name="password">abc</property> <property name="initialPoolSize">5</property> <property name="maxPoolSize">20</property> </default-config> </c3p0-config>
代碼如下:
/**
* 使用配置文件的方式
*/
public void demo2(){
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try{
/*// 獲得連接:
ComboPooledDataSource dataSource = new ComboPooledDataSource();*/
// 獲得連接:
// conn = dataSource.getConnection();
conn = JDBCUtils2.getConnection();
// 編寫Sql:
String sql = "select * from user";
// 預(yù)編譯SQL:
pstmt = conn.prepareStatement(sql);
// 設(shè)置參數(shù)
// 執(zhí)行SQL:
rs = pstmt.executeQuery();
while(rs.next()){
System.out.println(rs.getInt("uid")+" "+rs.getString("username")+" "+rs.getString("password")+" "+rs.getString("name"));
}
}catch(Exception e){
e.printStackTrace();
}finally{
JDBCUtils2.release(rs, pstmt, conn);
}
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Eclipse導(dǎo)入項(xiàng)目報(bào)錯(cuò)問(wèn)題解決方案
這篇文章主要介紹了Eclipse導(dǎo)入項(xiàng)目報(bào)錯(cuò)問(wèn)題解決方案,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-07-07
Myeclipse部署Tomcat_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
這篇文章給大家介紹了Myeclipse部署Tomcat的相關(guān)知識(shí),非常不錯(cuò),具有參考借鑒價(jià)值,需要的的朋友參考下吧2017-07-07
SpringBoot項(xiàng)目發(fā)送釘釘消息功能實(shí)現(xiàn)
在工作中的一些告警需要發(fā)送釘釘通知,有的是發(fā)給個(gè)人,有的要發(fā)到群里,這時(shí)項(xiàng)目就需要接入釘釘,實(shí)現(xiàn)發(fā)消息的功能,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2024-02-02
Idea跑的項(xiàng)目沒(méi)問(wèn)題將程序install成jar包運(yùn)行報(bào)錯(cuò)空指針的問(wèn)題
這篇文章主要介紹了Idea跑的項(xiàng)目沒(méi)問(wèn)題,將程序install成jar包運(yùn)行報(bào)錯(cuò)空指針的問(wèn)題,本文通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-06-06

