目录
什么是连接池?
- 一种存放数据库连接的容器,并且拥有动态新增连接、管理连接等功能于一体的容器。本质上来说,它就是个容器。
- 原理图示:
可以联想到现在的移动充电宝,需要用的时候随拿随用,用完还。
为什么要使用连接池
- 加快连接的获取速度
- 合理的应用连接
连接池分类
- dbcp
- c3p0
- druid
- hikaricp(常用,连接速度最快)
......
连接池的核心对象
连接池的核心对象是DataSource,所有的连接池都要继承DataSource。
连接池对象
此代码对应的数据库以及表格
/**
* 1、导包
* 2、创建连接池对象
* 3、从连接池中获取连接
* 4、使用连接
* 5、回收连接
*/
public class Demo {
public static void main(String[] args) throws Exception {
//1、创建连接池对象
HikariDataSource hikariDataSource = new HikariDataSource();
//驱动
hikariDataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
//统一资源定位符(jdbc:mysql:///jdbc(数据库名)?severTimezone=UTC(更改时区)
hikariDataSource.setJdbcUrl("jdbc:mysql:///jdbc?serverTimezone=UTC");
//Mysql账号密码
hikariDataSource.setUsername("root");
hikariDataSource.setPassword("root");
//2、获取连接
Connection connection = hikariDataSource.getConnection();
//使用JDBC工具类输,入sql语句,得到数据库中表中的字段的值
ResultSet resultSet = DBUtils.executeQuery("select * from t_user", connection);
while (resultSet.next()){
System.out.println(resultSet.getString("name"));
}
//3、回收连接
hikariDataSource.evictConnection(connection);
}
}
深入理解连接池工具类:
由HikariDataSource的源码可以看到,是它接收HikariConfig配置类的hikariConfig对象
new一个HikariConfig配置类的对象让HikariDataSource接收
由源码看到,HikariConfig配置类可以接收Properties对象
同理,new一个Properties对象给HikariConfig配置类接收
需要更改工具类的连接
获取t_user表中字段的数量
连接池工具类全部代码:
package blb.connectionPool;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import java.sql.*;
import java.util.Properties;
/**
*
* 工具类
*/
public class DBUtils {
//驱动
private static String driverClass = "com.mysql.cj.jdbc.Driver";
//统一资源定位符(jdbc:mysql:///jdbc(数据库名)?serverTimezone=UTC(更改时区))
private static String url="jdbc:mysql:///jdbc?serverTimezone=UTC";
private static String username = "root";
private static String password = "root";
private static HikariDataSource hikariDataSource;
/**
* 驱动
*/
static {
Properties properties = null;
try {
properties = new Properties();
//这里用的jdbc.properties配置文件
properties.load(DBUtils.class.getClassLoader().getResourceAsStream("jdbc.properties"));
driverClass = properties.getProperty("driverClassName");
url = properties.getProperty("jdbcUrl");
username = properties.getProperty("username");
password = properties.getProperty("password");
} catch (Exception e) {
e.printStackTrace();
System.out.println("未找到配置文件,使用默认配置!!");
properties.setProperty("driverClassName",driverClass);
properties.setProperty("jdbcUrl",url);
properties.setProperty("username",username);
properties.setProperty("password",password);
}
//初始化连接池对象
HikariConfig hikariConfig = new HikariConfig(properties);
//hikari是最快的连接池,所以用它
hikariDataSource= new HikariDataSource(hikariConfig);
try {
Class.forName(driverClass);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
/**
* 让每一个都获取一个连接
* @return
* @throws Exception
*/
public static Connection getConnection() throws Exception{
return hikariDataSource.getConnection();
}
/**
* 查询方法
* @param sql
* @param connection
* @throws SQLException
* @return
*/
public static ResultSet executeQuery(String sql, Connection connection,Object ...params) throws SQLException {
//获取预处理对象
PreparedStatement preparedStatement = connection.prepareStatement(sql);
//设置参数
for (int i = 0; i <params.length ; i++) {
preparedStatement.setObject(i+1,params[i]);
}
//执行sql
return preparedStatement.executeQuery();
}
/**
* 执行增删改方法
* @param sql
* @param connection
* @param params
* @throws SQLException
*/
public static int execute(String sql,Connection connection,Object... params) throws SQLException {
//获取预处理对象
PreparedStatement preparedStatement = connection.prepareStatement(sql);
//设置参数
for (int i = 0; i <params.length ; i++) {
preparedStatement.setObject(i+1,params[i]);
}
//执行sql
int len = preparedStatement.executeUpdate();
return len;
}
}
拓展
由HiKariConfig的源码可以看到,连接池除了在properties配置文件里面配置这四大核心属性,但实际上是还可以配置其它的很多很多的要素的,这些要素都有默认值。