连接池的基本语法
protected Connection con;
protected PreparedStatement ps;
protected ResultSet re;
/**連接池對象*/
private static BasicDataSource bds=new BasicDataSource();
static{
//設(shè)置驅(qū)動
bds.setDriverClassName("org.gjt.mm.mysql.Driver");
//設(shè)置連接URL
bds.setUrl("jdbc:mysql://localhost:3306/myitem?characterEncoding=utf-8");
//設(shè)置數(shù)據(jù)庫登錄用戶名
bds.setUsername("root");
//設(shè)置數(shù)據(jù)庫登錄密碼
bds.setPassword("1234");
//設(shè)置最大連接數(shù)
bds.setMaxActive(300);
//設(shè)置最少連接數(shù)
bds.setMaxIdle(100);
//設(shè)置最大等待時間
bds.setMaxWait(1000);
}
public void setConnection(){
try {
//從連接池中取出一個鏈接對象
this.con=bds.getConnection();
} catch (SQLException e) {
e.printStackTrace();
}
}
/**
* 關(guān)閉連接,將鏈接對象的狀態(tài)從忙碌變?yōu)榭臻e
*/
public void closeConnection(){
try {
if(re!=null){
re.close();
}
if(ps!=null){
ps.close();
}
if(con!=null){
con.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
轉(zhuǎn)載于:https://www.cnblogs.com/cj28-27/p/5492132.html
總結(jié)
- 上一篇: Java判断字符串的开头和结尾
- 下一篇: LINUX 文件系统如何存储文件 图解