package DatabaseConnectionPool;
import java.beans.PropertyVetoException;
import java.io.FileInputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
import com.mchange.v2.c3p0.ComboPooledDataSource;
public class DatabaseConnectionPool {
private static Properties pro=new Properties();
private static ComboPooledDataSource databasesource=new ComboPooledDataSource();
/*************初始化数据库连接池*************/
private static void initPool()
{
try {
pro.load(new FileInputStream("jdbc.properties"));
databasesource.setDriverClass(pro.getProperty("jdbc.driver"));
databasesource.setJdbcUrl(pro.getProperty("jdbc.url"));
databasesource.setUser(pro.getProperty("jdbc.usrName"));
databasesource.setPassword(pro.getProperty("jdbc.passwd"));
databasesource.setInitialPoolSize(15);
databasesource.setMinPoolSize(5);
databasesource.setMaxPoolSize(30);
databasesource.setMaxIdleTime(60);
databasesource.setIdleConnectionTestPeriod(60);
} catch (PropertyVetoException | IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/*************建立连接、创建Statement*************/
public static Statement getConnction()
{
initPool();
Connection conn=null;
Statement st=null;
try {
conn=databasesource.getConnection();
st=conn.createStatement();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return st;
}
}
JDBC学习笔记(6)之数据库连接池实现
最新推荐文章于 2021-08-11 22:01:30 发布