数据库连接池——c3p0和Druid的简单配置和测试
传统JDBC操作:
A:注册驱动
B:获取连接
C:获取预编译SQL对象
D:执行SQL语句
E:释放资源
在这个过程中,每次与数据库交互,都会频繁的注册驱动,获取连接…以及释放资源,对系统的系统造成极大的浪费
数据库连接池:
数据库连接池(connection pool)概念:本质上是个集合容器,里面存放着数据库的连接。
A:系统初始化时,创建一定数量的连接对象放入连接池。
B:当有需要时,从连接池中获取空闲的连接对象,对数据库进行操作
C:使用完毕后,将该连接对象归还至连接池,方便后续复用
数据库连接池的设计思想:消除频繁创建对象和释放资源带来的延迟,提高系统性能
目前主流的数据库连接池技术分为两种:c3p0和Druid
C3P0连接池:
官网下载地址,速度喜人
c3p0有两个jar包:c3p0-0.9.5.2.jar和mchange-commons-java-0.2.12.jar。
1:下载jar包,导入jar包
2:官方文档中有一段洋文,万能的百度翻译如是说道:配置文件通常在应用程序的application’s classpath下查找c3p0.properties或c3p0-config.xml,然而XML配置信息哪儿都可以放。
3:反正听得挺晕乎的,其实就是让你把c3p0.properties或c3p0-config.xml扔到src目录下就完事了。`
c3p0-config.xml
<?xml version="1.0" encoding="utf-8"?>
<c3p0-config>
<!--使用默认的配置-->
<default-config>
<property name="driverClass">com.mysql.jdbc.Driver</property>
<property name="jdbcUrl">jdbc:mysql://localhost:3306/bocai</property>
<property name="user">root</property>
<property name="password">root</property>
<!--initialPoolSize:初始化容量-->
<property name="initialPoolSize">5</property>
<!--maxPoolSize:连接池中最大容量-->
<property name="maxPoolSize">10</property>
<!--checkoutTimeout:超时时间(3秒钟)-->
<property name="checkoutTimeout">3000</property>
</default-config>
<named-config name="bocai">
</named-config>
</c3p0-config>
测试代码如下:
package C3P0;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import java.sql.Connection;
/**
* @author 菠菜饭团
* @title: PoolDemo
* @date 2020/11/24
*/
public class JDBCUtils{
static ComboPooledDataSource cpds = null;
static {
/*该方法会在c3p0-config.xml文件中寻找<named-config name="bocai">标签体中的
内容,并使用其内的配置信息*/
//cpds = new ComboPooledDataSource("bocai");
//创建数据库连接池对象,使用<named-config name="default-config">中的默认配置
cpds = new ComboPooledDataSource();
}
/**
* @description: 获取连接
* @author 菠菜饭团
* @date 2020/11/24
*/
public static Connection getConnection(){
try {
return cpds.getConnection();
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* @description: 释放连接
* @author 菠菜饭团
* @date 2020/11/24
*/
public static void close(Connection connection){
if (connection != null) {
try {
connection.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
/**
* @description: 测试连接
* @author 菠菜饭团
* @date 2020/11/24
*/
public static void main(String[] args) {
Connection connection = JDBCUtils.getConnection();
System.out.println(connection.getClass().getName());
JDBCUtils.close(connection);
}
}
控制台打印输出:com.mchange.v2.c3p0.impl.NewProxyConnection
哦,别忘了导入数据库驱动包:mysql-connector-java-5.1.37-bin.jar(数据库连接池没有数据库驱动包,肯定玩不转。)
Druid连接池:
jar包下载链接 提取码:bcft
1:导入jar包druid-1.0.9.jar
2:编写properties格式的配置文件,(推荐命名为druid.properties)
3:通过工厂类DruidDataSourceFactory来获取连接池对象
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/bocai
username=root
password=root
#初始容量
initialSize=5
#最大容量
maxActive=10
#超时时间
maxWait=3000
简单测试Demo:
private static Properties pro;
private static InputStream is;
private static DataSource ds;
static{
//定义配置文件
pro = new Properties();
is = JDBCUtils.class.getClassLoader().getResourceAsStream("druid.properties");
try {
//加载配置文件
pro.load(is);
//获取连接池对象
ds = DruidDataSourceFactory.createDataSource(pro);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* @description: 获取连接
* @author 菠菜饭团
* @date 2020/11/25
*/
public static Connection getConnection() throws SQLException {
return ds.getConnection();
}
/**
* @description: 获取数据源
* @author 菠菜饭团
* @date 2020/11/25
*/
public static DataSource getDataSource(){
return ds;
}
public static void main(String[] args) throws SQLException {
Connection conn = JDBCUtils.getConnection();
System.out.println(conn);
}
补充:JdbcTemplate
介绍:spring框架对jdbc的简单封装,提供了一个JdbcTemplate对象简化jdbc的开发
使用步骤:
A:导入jar包 下载地址 提取码:bcft
B:创建JdbcTemplate对象,该对象依赖于数据源DataSource
JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());
常用方法:
1: update():执行DML语句。增、删、改语句
2: queryForMap():将查询结果集封装为一个map集合,列名为key,值为value(该方法查询的结果集长度是1)
3: queryForList():将查询结果集封装为list集合(每条记录封装为一个Map集合,再将Map集合装载到List集合中)
4: query():查询结果,将结果封装为JavaBean对象
* query的参数:RowMapper
* 一般使用BeanPropertyRowMapper实现类。可以完成数据到JavaBean的自动封装
* new BeanPropertyRowMapper<类型>(类型.class)
5: queryForObject:查询结果,将结果封装为对象(一般用于聚合函数的查询)
简单演示:
JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());
String sql = "update user set email = '26016@bc.com' where username = ?";
int a = template.update(sql, "菠菜饭团");
String sql2 = "select * from user";
List<Map<String, Object>> list = template.queryForList(sql2);
String sql3 = "select * from emp";
List<User> userList = template.query(sql, new BeanPropertyRowMapper<User>(User.class));
String sql4 = "select count(id) from user";
Long total = template.queryForObject(sql4, Long.class);

浙公网安备 33010602011771号