连接池
市面上在Java领域中成熟的连接池产品有很多,常见的有: C3P0
DBCP
(老牌的) Druid
HikariCP
(新秀)
不论采用哪种连接池,最基本的四要素:
- 驱动程序
- URL地址
- 用户名
- 密码
C3P0
官网: https://www.mchange.com/projects/c3p0/
必须记忆: 它的核心类是
ComboPooledDataSource
public static void main(String[] args) throws SQLException {
ComboPooledDataSource cpds = new ComboPooledDataSource();
//池创建好以后,通过getConnection 方法就可以获取 可用的数据库的连接对象。
Connection connection = cpds.getConnection();
//中间的代码跟以前一模一样。
PreparedStatement preparedStatement = connection.prepareStatement("select * from tb1");
ResultSet resultSet = preparedStatement.executeQuery();
while(resultSet.next()){
System.out.println(resultSet.getString("c1"));
}
resultSet.close();
//这里要注意一下,这个拿到的connection对象它的close方法跟以前通过驱动管理器拿到的对象的close方法行为不一致。
//以前的close方法是方法连接资源。
//现在的close方法是将连接还给连接池。
connection.close();
//程序退出,释放池中的资源
cpds.close();
}
DBCP
官网: http://commons.apache.org/proper/commons-dbcp/
public static void main(String[] args) throws Exception {
//0. 通过配置文件加载 Properties 对象
Properties properties = new Properties();
properties.load(Demo.class.getResourceAsStream("dbcp-config.properties"));
//1. 记住使用核心对象 BasicDataSourceFactory 来进行构建
BasicDataSource dataSource = BasicDataSourceFactory.createDataSource(properties);
//2.通过连接池获取可用的连接
Connection connection = dataSource.getConnection();
//中间的代码跟以前一模一样。
PreparedStatement preparedStatement = connection.prepareStatement("select * from tb1");
ResultSet resultSet = preparedStatement.executeQuery();
while(resultSet.next()){
System.out.println(resultSet.getString("c1"));
}
resultSet.close();
//将连接放回池中
connection.close();
//关闭连接池
dataSource.close();
}
Druid
文档:
https://github.com/alibaba/druid/wiki/%E5%B8%B8%E8%A7%81%E9%97%AE%E9%A2%98
核心类 : DruidDataSource
public static void main(String[] args) throws IOException, SQLException {
Properties properties = new Properties();
//注意:配置项要使用 druid. 前缀
properties.load(Demo.class.getResourceAsStream("druid-config.properties"));
//使用核心对象
DruidDataSource dataSource= new DruidDataSource();
//通过properties配置文件来进行配置
dataSource.configFromPropety(properties);
//2.通过连接池获取可用的连接
Connection connection = dataSource.getConnection();
//中间的代码跟以前一模一样。
PreparedStatement preparedStatement = connection.prepareStatement("select * from tb1");
ResultSet resultSet = preparedStatement.executeQuery();
while(resultSet.next()){
System.out.println(resultSet.getString("c1"));
}
resultSet.close();
//将连接放回池中
connection.close();
dataSource.close();
}
HikariCP
官网: https://github.com/brettwooldridge/HikariCP
核心对象: HikariDataSource
注意:它还依赖 slf4j-api-1.7.25.jar
jar包
public static void main(String[] args) throws SQLException {
// Properties properties = new Properties();
// properties.load(Demo.class.getResourceAsStream("hikaricp-config.properties"));
// HikariConfig hikariConfig = new HikariConfig(properties);
HikariConfig hikariConfig = new HikariConfig("/com/example/hikaricp/hikaricp-config.properties");
HikariDataSource dataSource = new HikariDataSource(hikariConfig);
//2.通过连接池获取可用的连接
Connection connection = dataSource.getConnection();
//中间的代码跟以前一模一样。
PreparedStatement preparedStatement = connection.prepareStatement("select * from tb1");
ResultSet resultSet = preparedStatement.executeQuery();
while(resultSet.next()){
System.out.println(resultSet.getString("c1"));
}
resultSet.close();
//将连接放回池中
connection.close();
dataSource.close();
}