@TOC
数据库连接池
概念
就是一个容器(集合),存放数据库连接的容器
当系统初始化后,容器被创建,容器会申请一些连接对象,当用户访问数据库的时候,从容器中获取连接对象,用户访问完后,会将连接对象归还容器。
好处
- 节约资源
- 用户访问高效
实现
标准接口:DataSource
- 方法:getConnection()
一般我们不用实现它,由数据库厂商提供
- c3p0:数据连接池数据实现
- Druid:数据库连接池实现技术,由阿里巴巴提供
c3p0:数据库连接池
使用步骤
- 导入jar包
- 定义配置文件
- 名称:c3p0.properties或c3p0-config.xml
- 路径:直接将文件放到src目录下
- 创建核心对象,数据库连接池对象 ComboPooledDataSource
- 获取连接:getConnection
public static void main(String[] args) {
Connection conn=null;
/*1 创建数据库连接池对象*/
ComboPooledDataSource ds = new ComboPooledDataSource();
/*2 获取连接对象*/
try {
conn = ds.getConnection();
} catch (SQLException e) {
e.printStackTrace();
}
System.out.println("conn:"+conn);
}
xml配置文件内容
<c3p0-config>
<!-- 使用默认的配置读取连接池对象 -->
<default-config>
<!-- 连接参数 -->
<property name="driverClass">com.mysql.cj.jdbc.Driver</property>
<property name="jdbcUrl">jdbc:mysql://localhost:3306/test</property>
<property name="user">root</property>
<property name="password">123456</property>
<!-- 连接池参数 -->
<!--初始化的申请的连接数量-->
<property name="initialPoolSize">5</property>
<!--最大的连接数量-->
<property name="maxPoolSize">10</property>
<!--连接超时时间-->
<property name="checkoutTimeout">3000</property>
</default-config>
<named-config name="otherc3p0">
<!-- 连接参数 -->
<property name="driverClass">com.mysql.cj.jdbc.Driver</property>
<property name="jdbcUrl">jdbc:mysql://localhost:3306/test</property>
<property name="user">root</property>
<property name="password">123456</property>
<!-- 连接池参数 -->
<property name="initialPoolSize">5</property>
<property name="maxPoolSize">8</property>
<property name="checkoutTimeout">1000</property>
</named-config>
</c3p0-config>
Druid数据库连接池
步骤
- 导入jar包
- 定义配置文件
- 是properties形式
- 可以放在任意目录下,任意的名称
- 加载配置文件
- 获取数据库连接池对象:通过工厂来获取 DruidDataSourceFactory
- 获取连接:getConnection
public static void main(String[] args) throws Exception {
/*1 导入jar包*/
/*2 定义配置文件*/
/*3 加载配置文件*/
Properties pro = new Properties();
InputStream is = druidDemo1.class.getClassLoader().getResourceAsStream("druid.properties");
try {
pro.load(is);
} catch (IOException e) {
e.printStackTrace();
}
/*4 获取连接池对象*/
DataSource ds = DruidDataSourceFactory.createDataSource(pro);
/*5 获取连接*/
Connection conn = ds.getConnection();
System.out.println("conn:"+conn);
}
配置文件
driverClassname=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/test
username=root
password=123456
initialSize=5
maxActive=10
maxWait=3000
定义工具类
- 定义一个JDBCUtils
- 提供静态代码块加载配置文件,初始化连接池对象
- 提供方法
- 获取连接方法:通过数据库连接池获取连接
- 释放资源
- 获取连接池的方法
/**
* druid连接池工具类
* */
public class JDBCUtils {
/*1 定义变量名称 DataSource*/
private static DataSource ds;
static {
try {
/*1 加载配置文件*/
Properties pro = new Properties();
pro.load(JDBCUtils.class.getClassLoader().getResourceAsStream("druid.properties"));
/*2 获取dataSource*/
ds = DruidDataSourceFactory.createDataSource(pro);
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 获取连接
*/
public static Connection getConnection() throws SQLException {
return ds.getConnection();
}
/**
* 释放资源
* */
public static void close(Statement stmt ,Connection conn){
/*if (stmt !=null){
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (conn !=null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}*/
close(null,stmt,conn);
}
public static void close(ResultSet res, Statement stmt , Connection conn){
if (res !=null){
try {
res.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (stmt !=null){
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (conn !=null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
/**
* 获取连接池方法
* */
public static DataSource getDataSource(){
return ds;
}
}
Spring JDBC
spring框架对JDBC的简单封装。
提供了要给JDBCTemplate对象简化JDBC的开发
步骤
- 导入jar
- 创建JDBCTemplate对象,依赖于DataSource
- 调用JDBCTemplate方法来完成CRUD的操作
- update():执行DML语句。增、删、改语句
- queryForMap():查询结果将结果封装为map集合:查询的结果集长度只能是1
- queryForList():查询结果将结果封装为list集合:查询的每条数据封装成map集合,之后再放到list集合中
- query():查询结果,将结果封装为JavaBean对象
- queryForObject():查询结果,将结果封装为对象
public class JdbcTemplateDemo2 {
/*1 获取JDBCTemplate对象*/
private JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());
@Test
public void test1(){
/*1 获取JDBCTemplate对象*/
/*2 定义sql*/
String sql = "update account set balance = 3000 where id = ?";
/*3 执行sql*/
int count = template.update(sql, 4);
System.out.println(count);
}
@Test
public void test2(){
/*1 获取JDBCTemplate对象*/
/*2 定义sql*/
String sql = "insert into account values(? ,?,?)";
/*3 执行sql*/
int count = template.update(sql, null,"李清照",400);
System.out.println(count);
}
@Test
public void test3(){
/*1 获取JDBCTemplate对象*/
/*2 定义sql*/
String sql = "delete from account where id = ?";
/*3 执行sql*/
int count = template.update(sql, 8);
System.out.println(count);
}
/**
* 注意:查询的结果长度只能是1
* */
@Test
public void test4(){
/*1 获取JDBCTemplate对象*/
/*2 定义sql*/
String sql = "select * from account where id = ?";
/*3 执行sql*/
Map<String, Object> map = template.queryForMap(sql, 2);
System.out.println(map);
}
@Test
public void test5(){
/*1 获取JDBCTemplate对象*/
/*2 定义sql*/
String sql = "select * from account ";
/*3 执行sql*/
List<Map<String, Object>> list = template.queryForList(sql);
System.out.println(list);
}
@Test
public void test6(){
/*1 获取JDBCTemplate对象*/
/*2 定义sql*/
String sql = "select * from account ";
/*3 执行sql*/
List<Emp> list = template.query(sql, new RowMapper<Emp>() {
@Override
public Emp mapRow(ResultSet rs, int i) throws SQLException {
Emp emp = new Emp();
int id = rs.getInt("id");
String username = rs.getString("username");
int balance = rs.getInt("balance");
emp.setId(id);
emp.setBalance(balance);
emp.setUsername(username);
return emp;
}
});
for (Emp emp : list) {
System.out.println(emp);
}
}
@Test
public void test7(){
/*1 获取JDBCTemplate对象*/
/*2 定义sql*/
String sql = "select * from account ";
/*3 执行sql*/
List<Emp> list = template.query(sql,new BeanPropertyRowMapper<Emp>(Emp.class));
for (Emp emp : list) {
System.out.println(emp);
}
}
@Test
public void test8(){
/*1 获取JDBCTemplate对象*/
/*2 定义sql*/
String sql = "select count(id) from account ";
/*3 执行sql*/
Long aLong = template.queryForObject(sql, Long.class);
System.out.println(aLong);
}
}