1.方式
一種是使用直接c3p0數(shù)據(jù)庫(kù)連接池轮蜕,一種是使用配置文件將連接信息寫(xiě)在配置文件中。
將連接信息寫(xiě)在配置文件中有很多好處叽奥,比如如果想替換一個(gè)線上項(xiàng)目的數(shù)據(jù)庫(kù)赎线,只要改變配置文件中的鏈接信息就可以了。再就是一些需要修改的地方钥弯,比如想要修改鏈接池的屬性径荔,也只需要修配置文件就可以了,不需要去修改java代碼脆霎,那樣還需要重新編譯生成class文件总处,重新運(yùn)行一遍。
2.直接使用jar包
睛蛛、辨泳、、玖院、、第岖、难菌、、蔑滓、郊酒、、键袱、燎窘、、蹄咖、褐健、、澜汤、蚜迅、、俊抵、谁不、、徽诲、刹帕、吵血、、偷溺、蹋辅、、亡蓉、晕翠、、砍濒、淋肾、、爸邢、樊卓、、杠河、碌尔、
package com.begin21.w1106.ClassTest.excute;
import java.beans.PropertyVetoException;
import java.sql.Connection;
import java.sql.SQLException;
import com.mchange.v2.c3p0.ComboPooledDataSource;
//數(shù)據(jù)庫(kù)連接池
public class ConnectionPool {
private static ComboPooledDataSource comboPooledDataSource;
//初始化連接池
public static final void initDBSource(){
comboPooledDataSource = new ComboPooledDataSource();
try {
comboPooledDataSource.setDriverClass("com.mysql.jdbc.Driver");
} catch (PropertyVetoException e) {
e.printStackTrace();
}
comboPooledDataSource.setJdbcUrl("jdbc:mysql://localhost:3306/gaobo");
comboPooledDataSource.setUser("root");
comboPooledDataSource.setPassword("");
comboPooledDataSource.setMinPoolSize(3); //最小線程數(shù)
comboPooledDataSource.setMaxPoolSize(20);//最大線程數(shù)
comboPooledDataSource.setInitialPoolSize(5);//初始線程數(shù)
comboPooledDataSource.setAcquireIncrement(2);//每次新增線程數(shù)
comboPooledDataSource.setMaxIdleTime(5);//線程空閑時(shí)間
}
/**
* 從連接池中獲取連接
* @return
*/
public static synchronized Connection getConnection() {
Connection conn = null;
try {
conn = comboPooledDataSource.getConnection();
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
public static void closeOpenResource(AutoCloseable resource) {
if (null != resource) {
try {
resource.close();
} catch (Exception e) {
e.printStackTrace();
}}}}
、券敌、唾戚、、待诅、叹坦、、卑雁、募书、、测蹲、莹捡、、扣甲、篮赢、、琉挖、荷逞、、粹排、种远、、顽耳、坠敷、妙同、、膝迎、粥帚、、限次、芒涡、、卖漫、费尽、、羊始、旱幼、、突委、柏卤、、
package com.begin21.w1106.ClassTest.excute;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class Main2 {
public static void main(String[] args) {
ConnectionPool.initDBSource();
query();}
private static void query() {
Connection conn = ConnectionPool.getConnection();
PreparedStatement prst = null;
ResultSet rs = null;
String sql = "select * from repo_sto";
try {
prst = conn.prepareStatement(sql);
rs = prst.executeQuery();
while(rs.next()) {
System.out.println(rs.getString(2));}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
ConnectionPool.closeOpenResource(rs);
ConnectionPool.closeOpenResource(prst);
// close不是關(guān)閉數(shù)據(jù)庫(kù)連接池的連接對(duì)象匀油,而是把它放回連接池
ConnectionPool.closeOpenResource(conn);}}}
3.使用配置文件
配置文件 db.properties
package com.begin21.w1106.ClassTest.excute;
import java.beans.PropertyVetoException;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;
import com.mchange.v2.c3p0.ComboPooledDataSource;
public class ConnectionPoolProp {
private static ComboPooledDataSource comboPooledDataSource;
public static final void initDBSource() {
Properties properties = new Properties();
try {
File file = new File(ConnectionPoolProp.class.getResource("/").getFile().toString() + "dbs.properties"); ?//引用配置文件
System.out.println(ConnectionPoolProp.class.getResource("/").getFile());
InputStream inputStream = new FileInputStream(file);
properties.load(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
comboPooledDataSource = new ComboPooledDataSource();
try {
comboPooledDataSource.setDriverClass(properties.getProperty("driver"));
} catch (PropertyVetoException e) {
e.printStackTrace();
}
comboPooledDataSource.setJdbcUrl(properties.getProperty("url"));
comboPooledDataSource.setUser(properties.getProperty("user"));
comboPooledDataSource.setPassword(properties.getProperty("password"));
comboPooledDataSource.setMinPoolSize(Integer.parseInt(properties.getProperty("c3p0.minPoolSize")));
comboPooledDataSource.setMaxPoolSize(Integer.parseInt(properties.getProperty("c3p0.maxPoolSize")));
comboPooledDataSource.setInitialPoolSize(Integer.parseInt(properties.getProperty("c3p0.initPoolSize")));
comboPooledDataSource.setAcquireIncrement(Integer.parseInt(properties.getProperty("c3p0.acquireIncrement")));
comboPooledDataSource.setMaxStatements(Integer.parseInt(properties.getProperty("c3p0.maxStatements").trim()));
comboPooledDataSource.setMaxIdleTime(Integer.parseInt(properties.getProperty("c3p0.maxIdleTime")));
}
public static synchronized Connection getConnection() {
Connection conn = null;
try {
conn = comboPooledDataSource.getConnection();
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
public static void closeOpenResource(AutoCloseable resource) {
if (null != resource) {
try {
resource.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();}}}}