一. 概念:
数据库连接池其实就是一个容器(集合),存放数据库连接的容器。当系统初始化好后,容器被创建,容器中会申请一些连接对象,当用户来访问数据库时,从容器中获取连接对象,用户访问完之后,会将连接对象归还给容器。
二、作用
1.资源重用,避免了数据库连接频繁建立、关闭的开销
2.更快的系统响应速度,直接从连接池中获取连接,响应速度加快
3.控制资源的使用。如果不使用连接池,每次访问数据库都需要创建一个连接,这样系统的稳定性受系统连接需求影响很大,很容易产生资源浪费和高负载异常。连接池能够使性能最大化,将资源利用控制在一定的水平之下。连接池能控制池中的连接数量,增强了系统在大量用户应用时的稳定性。
三、常用的数据库连接池:DBCP、C3P0、Druid
四、如何实现
1. 标准接口:DataSource javax.sql包下的
1. 方法:
* 获取连接:getConnection()
* 归还连接:Connection.close()。如果连接对象Connection是从连接池中获取的,那么调用Connection.close()方法,则不会再关闭连接了。而是归还连接
2. 一般我们不去实现它,由数据库厂商来实现
1. C3P0:数据库连接池技术
2. Druid:数据库连接池实现技术,由阿里巴巴提供的
4. C3P0:数据库连接池技术
* 步骤:
1. 导入jar包 (两个) c3p0-0.9.5.2.jar mchange-commons-java-0.2.12.jar ,
* 不要忘记导入数据库驱动jar包
2. 定义配置文件:
* 名称: c3p0.properties 或者 c3p0-config.xml
* 路径:直接将文件放在src目录下即可。
3. 创建核心对象 数据库连接池对象 ComboPooledDataSource
4. 获取连接: getConnection
* 代码:
//1.创建数据库连接池对象
DataSource ds = new ComboPooledDataSource();
//2. 获取连接对象
Connection conn = ds.getConnection();
5. Druid:数据库连接池实现技术,由阿里巴巴提供的
1. 步骤:
1. 导入jar包 druid-1.0.9.jar
2. 定义配置文件:
* 是properties形式的
* 可以叫任意名称,可以放在任意目录下
3. 加载配置文件。Properties
4. 获取数据库连接池对象:通过工厂来来获取 DruidDataSourceFactory
5. 获取连接:getConnection
注意:我们我们使用数据库工具类技术实现
2. 定义工具类
1. 定义一个类 JDBCUtils
2. 提供静态代码块加载配置文件,初始化连接池对象
3. 提供方法
1. 获取连接方法:通过数据库连接池获取连接
2. 释放资源
3. 获取连接池的方法
五、举例
druid.properties
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://127.0.0.1:3306/myemployees
username=root
password=tt1314521
#初始化连接池连接个数
initialSize=5
#连接池最大连接个数
maxActive=10
#连接池连接等待时间
maxWait=3000
DruidUtils.java
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidDataSourceFactory;
import javax.sql.DataSource;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;
/**
* 创建工具列,方便使用
*/
public class DruidUtils {
private static DataSource ds;
static{
try {
//获取配置文件
Properties pro = new Properties();
pro.load( DruidUtils.class.getClassLoader().getResourceAsStream("druid.properties"));
//获取DataSource---DataSource可以看作数据源,它封装了数据库参数,连接数据库,
// 程序中操作DataSource对象即可对数据库进行增删改查操作
ds = DruidDataSourceFactory.createDataSource(pro);
} catch (IOException e) {
e.printStackTrace();
}catch (Exception e) {
e.printStackTrace();
}
}
/**
* 获取连接
* @return
* @throws SQLException
*/
public static Connection getConnect() throws SQLException {
return ds.getConnection();
}
/**
*
* @param rs
* @param pd
* @param conn
*/
public static void close(ResultSet rs, PreparedStatement pd, Connection conn){
if(rs != null){
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(pd != null){
try {
pd.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(conn != null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
测试类:Demo01Template.java
import com.net.lwgk.util.DruidUtils;
import com.net.lwgk.util.JDBCUtils;
import org.testng.annotations.Test;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import java.util.List;
import java.util.Map;
/**
* 修改第一号数据,将Salary设置为10000
* (2)、添加一条记录
* (3)、删除刚才添加的记录
* (4)、查询id为 100的记录,并封装为Map集合
* (5)、查询所有记录,将其封装为List
* (6)、查询所有记录,封装为自定义类emp对象的Lis t在集合
* (7)、查询总记录数
*/
public class Demo01Template {
private JdbcTemplate template = new JdbcTemplate(DruidUtils.getDataSource());
/**
*
*/
@Test
public void test01(){
//JdbcTemplate template = new JdbcTemplate(DruidUtils.getDataSource());
String sql = "update employees set salary = 30000 where employee_id = 100";
template.update(sql);
}
/**
* 添加一条记录
*/
@Test
public void test02(){
//template = new JdbcTemplate(DruidUtils.getDataSource());
String sql = "INSERT INTO employees(employee_id,first_name,last_name,job_id,manager_id,department_id) VALUES(301,?,?,?,101,100)";
template.update(sql,"san","li","IT_PROG");
}
/**
* 删除刚才添加的记录
*/
@Test
public void test03(){
//template = new JdbcTemplate(DruidUtils.getDataSource());
String sql = "delete from employees where employee_id =?";
template.update(sql,301);
}
/**
* 查询id为 100的记录,并封装为Map集合
*/
@Test
public void test04(){
//template = new JdbcTemplate(DruidUtils.getDataSource());
String sql = "select * from employees where employee_id =?";
Map<String, Object> map = template.queryForMap(sql, 100);
System.out.println(map);
}
/**
* 查询所有记录,将其封装为List
*/
@Test
public void test05(){
//template = new JdbcTemplate(DruidUtils.getDataSource());
String sql = "select * from employees";
List<Map<String, Object>> maps = template.queryForList(sql);
for (Map<String, Object> map : maps) {
System.out.println(map);
}
}
/**
* 查询所有记录,封装为自定义类emp对象的Lis t在集合
* 返回的都是实际的对象集合
*/
@Test
public void test06(){
//template = new JdbcTemplate(DruidUtils.getDataSource());
String sql = "select * from employees";
List<employees> query = template.query(sql, new BeanPropertyRowMapper<employees>(employees.class));
for (employees employees : query) {
//System.out.println(employees);
System.out.println(employees.getLast_name());
}
}
/**
* 查询总记录数
*/
@Test
public void test07(){
//template = new JdbcTemplate(DruidUtils.getDataSource());
String sql = "select count(employee_id) from employees";
Long aLong = template.queryForObject(sql, Long.class);
System.out.println(aLong);
}
}