文章目录
- 数据库连接池
- C3P0:数据库连接池技术
- Druid:数据库连接池实现技术,由阿里巴巴提供的
- Spring JDBC
数据库连接池
- 概念:其实就是一个容器(集合),存放数据库连接的容器。
当系统初始化好后,容器被创建,容器中会申请一些连接对象,当用户来访问数据库时,从容器中获取连接对象,用户访问完之后,会将连接对象归还给容器。 - 好处:
- 节约资源
- 用户访问高效
- 实现:
- 标准接口:DataSource javax.sql包下的
- 方法:
- 获取连接:getConnection()
- 归还连接:Connection.close()。如果连接对象Connection是从连接池中获取的,那么调用Connection.close()方法,则不会再关闭连接了。而是归还连接
- 一般我们不去实现它,有数据库厂商来实现
- C3P0:数据库连接池技术
- Druid:数据库连接池实现技术,由阿里巴巴提供的
C3P0:数据库连接池技术
- 步骤:
1. 导入jar包 (两个) c3p0-0.9.5.2.jar mchange-commons-java-0.2.12.jar
* 网盘链接 提取码:gbjj
* 不要忘记导入数据库驱动jar包
3. 定义配置文件:
* 名称: c3p0.properties 或者 c3p0-config.xml
* 路径:直接将文件放在src目录下即可。
4. 创建核心对象 数据库连接池对象 ComboPooledDataSource
5. 获取连接: getConnection - 代码:
c3p0-config.xml:
<c3p0-config>
<!-- 使用默认的配置读取连接池对象 -->
<default-config>
<!-- 连接参数 -->
<property name="driverClass">com.mysql.jdbc.Driver</property>
<property name="jdbcUrl">jdbc:mysql://localhost:3306/db4</property>
<property name="user">root</property>
<property name="password">root</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.jdbc.Driver</property>
<property name="jdbcUrl">jdbc:mysql://localhost:3306/db1</property>
<property name="user">root</property>
<property name="password">root</property>
<!-- 连接池参数 -->
<property name="initialPoolSize">5</property>
<property name="maxPoolSize">8</property>
<property name="checkoutTimeout">1000</property>
</named-config>
</c3p0-config>
java测试代码:
package com.lj.c3p0;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
public class C3P0Demo2 {
public static void main(String[] args) throws SQLException {
//1.创建数据库连接对象,使用默认配置
//DataSource ds = new ComboPooledDataSource();
//使用指定名称的配置
DataSource ds = new ComboPooledDataSource("otherc3p0");
//2.获取连接对象
for (int i = 0; i < 10; i++) {
Connection conn = ds.getConnection();
//3.打印
System.out.println(conn);
if(i==5)
conn.close();
}
}
}
Druid:数据库连接池实现技术,由阿里巴巴提供的
- 步骤:
- 导入jar包 druid-1.0.9.jar
网盘链接 提取码:bw7h
CSDN资源 - 定义配置文件:
- 是properties形式的
- 可以叫任意名称,可以放在任意目录下
url=jdbc:mysql:///db4
username=root
password=root
driverClassName=com.mysql.cj.jdbc.Driver
# 初始化连接数量
initialSize=10
# 最大连接数
maxActive=20
# 最大等待时间
maxWait=1000
filters=wall
- 加载配置文件。Properties
- 获取数据库连接池对象:通过工厂来来获取 DruidDataSourceFactory
- 获取连接:getConnection
代码一:
package com.lj.druid;
import com.alibaba.druid.pool.DruidDataSourceFactory;
import javax.sql.DataSource;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.sql.Connection;
import java.util.Properties;
public class DruidDemo1 {
public static void main(String[] args) throws Exception {
//1.导入jar包
//2.定义配置文件
//3.加载配置文件
Properties pro = new Properties();
InputStream is = DruidDemo1.class.getClassLoader().getResourceAsStream("druid.properties");
pro.load(is);
//4.获取连接池对象
DataSource ds = DruidDataSourceFactory.createDataSource(pro);
//5.获取连接
Connection conn = ds.getConnection();
System.out.println(conn);
}
}
代码二:
package com.lj.druid;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class DruidDemo2 {
public static void main(String[] args) {
//给account表添加一条记录
//1.获取连接
Connection conn = null;
PreparedStatement pstmt = null;
try {
conn = JDBCutils.getConnection();
//2.定义sql
String sql= "insert into account values(null,?,?)";
//3.获取pstmt对象
pstmt= conn.prepareStatement(sql);
//4.给?赋值
pstmt.setString(1,"汪汪");
pstmt.setDouble(2,4000);
//5.执行sql
int count = pstmt.executeUpdate();
System.out.println(count);
} catch (SQLException throwables) {
throwables.printStackTrace();
}finally{
JDBCutils.close(pstmt,conn);
}
}
}
- 定义工具类
- 定义一个类 JDBCUtils
- 提供静态代码块加载配置文件,初始化连接池对象
- 提供方法
- 获取连接方法:通过数据库连接池获取连接
- 释放资源
- 获取连接池的方法
代码:
package com.lj.druid;
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.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
public class JDBCutils {
//1.定义成员变量 DataSource
private static DataSource ds;
static {
try {
//1.加载配置文件
Properties pro = new Properties();
pro.load(DruidDemo1.class.getClassLoader().getResourceAsStream("druid.properties"));
//2.获取连接池对象 DataSource
ds= DruidDataSourceFactory.createDataSource(pro);
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 获取链接
*
* @return
*/
public static Connection getConnection() throws SQLException {
return ds.getConnection();
}
/**
* 释放资源
*
* @param rs
* @param stmt
* @param conn
*/
public static void close(ResultSet rs, Statement stmt, Connection conn) {
if (rs != null) {
try {
rs.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if (stmt != null) {
try {
stmt.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if (conn != null) {
try {
conn.close(); //归还链接
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
public static void close(Statement stmt, Connection conn) {
if (stmt != null) {
try {
stmt.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if (conn != null) {
try {
conn.close(); //归还链接
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
public static DataSource getDataSource(){
return ds;
}
}
Spring JDBC
- Spring框架对JDBC的简单封装。提供了一个JDBCTemplate对象简化JDBC的开发
- 步骤:
- 导入jar包
- 创建JdbcTemplate对象。依赖于数据源DataSource
- JdbcTemplate template = new JdbcTemplate(ds);
- 调用JdbcTemplate的方法来完成CRUD的操作
- update():执行DML语句。增、删、改语句
- queryForMap():查询结果将结果集封装为map集合,将列名作为key,将值作为value 将这条记录封装为一个map集合
- 注意:这个方法查询的结果集长度只能是1
- queryForList():查询结果将结果集封装为list集合
- 注意:将每一条记录封装为一个Map集合,再将Map集合装载到List集合中
- query():查询结果,将结果封装为JavaBean对象
- query的参数:RowMapper
- 一般我们使用BeanPropertyRowMapper实现类。可以完成数据到JavaBean的自动封装
- new BeanPropertyRowMapper<类型>(类型.class)
- queryForObject:查询结果,将结果封装为对象
- 一般用于聚合函数的查询
- 练习:
- 需求:
- 修改1号数据的 salary 为 10000
- 添加一条记录
- 删除刚才添加的记录
- 查询id为1的记录,将其封装为Map集合
- 查询所有记录,将其封装为List
- 查询所有记录,将其封装为Emp对象的List集合
- 查询总记录数
表格:
代码实现:
Emp类:
package domain;
import java.util.Date;
public class Emp {
private int id;
private String ename;
private int job_id;
private int mgr;
private Date joindate;
private double salary;
private double bonus;
private int dept_id;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getEname() {
return ename;
}
public void setEname(String ename) {
this.ename = ename;
}
public int getJob_id() {
return job_id;
}
public void setJob_id(int job_id) {
this.job_id = job_id;
}
public int getMgr() {
return mgr;
}
public void setMgr(int mgr) {
this.mgr = mgr;
}
public Date getJoindate() {
return joindate;
}
public void setJoindate(Date joindate) {
this.joindate = joindate;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public int getDept_id() {
return dept_id;
}
public void setDept_id(int dept_id) {
this.dept_id = dept_id;
}
public double getBonus() {
return bonus;
}
public void setBonus(double bonus) {
this.bonus = bonus;
}
@Override
public String toString() {
return "Emp{" +
"id=" + id +
", ename='" + ename + '\'' +
", job_id=" + job_id +
", mgr=" + mgr +
", joindate=" + joindate +
", salary=" + salary +
", bonus=" + bonus +
", dept_id=" + dept_id +
'}';
}
}
JDBC工具类:JDBCutils.java
package domain;
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.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
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();
}
}
/**
* 获取链接
*
* @return
*/
public static Connection getConnection() throws SQLException {
return ds.getConnection();
}
/**
* 释放资源
*
* @param rs
* @param stmt
* @param conn
*/
public static void close(ResultSet rs, Statement stmt, Connection conn) {
if (rs != null) {
try {
rs.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if (stmt != null) {
try {
stmt.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if (conn != null) {
try {
conn.close(); //归还链接
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
public static void close(Statement stmt, Connection conn) {
if (stmt != null) {
try {
stmt.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if (conn != null) {
try {
conn.close(); //归还链接
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
public static DataSource getDataSource(){
return ds;
}
}
主程序:
package domain;
import org.junit.Test;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import java.sql.Date;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;
public class JdbcTemplateDemo2 {
//Junit单元测试,可以让方法独立运行
//1.获取JDBCTemplate对象
private JdbcTemplate template = new JdbcTemplate(JDBCutils.getDataSource());
/**
* 1.修改1号数据的salary为10000
*/
@Test
public void test1(){
//2.定义sql
String sql = "update emp set salary = 10000 where id = 1001";
//3.执行sql
System.out.println(template.update(sql));
}
/**
* 2.添加一条记录
*/
@Test
public void test2(){
//2.定义sql
String sql = "insert into emp(id,ename,dept_id) values(?,?,?) ";
//3.执行sql
System.out.println(template.update(sql,1015,"郭靖",10));
}
/**
* 3.删除id=1013的人记录
*/
@Test
public void test3(){
//2.定义sql
String sql = "delete from emp where id =?";
//3.执行sql
System.out.println(template.update(sql,1013));
}
/**
* 4.查询id为1001的记录,将其封装为Map集合
*
*/
@Test
public void test4(){
//2.定义sql
String sql = "select * from emp where id = ?";
//3.执行sql
Map<String, Object> map = template.queryForMap(sql, 1001);
//Map<String, Object> map = template.queryForMap(sql, 1001,1002); //这样会出错,这个方法的查询长度只能是1
System.out.println("test4:\n"+map+"\n-----------------------");
}
/**
* 5.查询所有集合将其封装为List集合
*/
@Test
public void test5(){
//2.定义sql
String sql = "select * from emp";
//3.执行sql
List<Map<String, Object>> list = template.queryForList(sql);//将每一条记录封装为一个Map集合,再将Map集合装在到List集合中
//Map<String, Object> map = template.queryForMap(sql, 1001,1002); //这样会出错,这个方法的查询长度只能是1
System.out.println("\ntest5:");
for(Map<String, Object> stringObjectMap : list)
{
System.out.println(stringObjectMap);
}
}
/**
* 6.查询所有记录,将其封装为Emp对象的List集合
*/
@Test
public void test6_1(){
String sql = "select * from emp";
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 ename = rs.getString("ename");
int job_id = rs.getInt("job_id");
int mgr = rs.getInt("mgr");
Date joindate = rs.getDate("joindate");
double salary = rs.getDouble("salary");
double bonus = rs.getDouble("bonus");
int dept_id = rs.getInt("dept_id");
emp.setId(id);
emp.setEname(ename);
emp.setJob_id(job_id);
emp.setMgr(mgr);
emp.setJoindate(joindate);
emp.setSalary(salary);
emp.setBonus(bonus);
emp.setDept_id(dept_id);
return emp;
}
});
System.out.println("\ntest6_1:");
for (Emp emp : list) {
System.out.println(emp);
}
}
// /**
// * 6.查询所有记录,将其封装为Emp对象的List集合
// * 这个方法会出错是因为Emp类中的成员变量需要把基本数据类型改成引用数据类型之后,效果就跟6_1的一样了。
// */
// @Test
// public void test6_2(){
// String sql = "select * from emp";
// List<Emp> list = template.query(sql,new BeanPropertyRowMapper<Emp>(Emp.class));
// System.out.println("\ntest6_2:");
// for (Emp emp : list) {
// System.out.println(emp);
// }
//
// }
/**
* 7.查询总记录数
*/
@Test
public void test7(){
String sql = "select count(id) from emp";
Long total = template.queryForObject(sql, Long.class);
System.out.println("\ntest7:");
System.out.println(total);
}
}
主程序中用到了Iunit单元测试(每一个方法上面有一个@Test),可以单独测试某一个方法。
执行结果:
"D:\Program Files\Java\jdk-11.0.9\bin\java.exe" -ea -Didea.test.cyclic.buffer.size=1048576 "-javaagent:D:\program files\JetBrains\IntelliJ IDEA 2020.3.1\lib\idea_rt.jar=60536:D:\program files\JetBrains\IntelliJ IDEA 2020.3.1\bin" -Dfile.encoding=UTF-8 -classpath "D:\program files\JetBrains\IntelliJ IDEA 2020.3.1\lib\idea_rt.jar;D:\program files\JetBrains\IntelliJ IDEA 2020.3.1\plugins\junit\lib\junit5-rt.jar;D:\program files\JetBrains\IntelliJ IDEA 2020.3.1\plugins\junit\lib\junit-rt.jar;E:\IdeaProjects\day6\out\production\spring_jdbc;E:\IdeaProjects\day6\spring_jdbc\libs\commons-logging-1.2.jar;E:\IdeaProjects\day6\spring_jdbc\libs\spring-beans-5.0.0.RELEASE.jar;E:\IdeaProjects\day6\spring_jdbc\libs\spring-core-5.0.0.RELEASE.jar;E:\IdeaProjects\day6\spring_jdbc\libs\spring-jdbc-5.0.0.RELEASE.jar;E:\IdeaProjects\day6\spring_jdbc\libs\spring-tx-5.0.0.RELEASE.jar;E:\IdeaProjects\day6\spring_jdbc\libs\c3p0-0.9.5.5.jar;E:\IdeaProjects\day6\spring_jdbc\libs\druid-1.1.22.jar;E:\IdeaProjects\day6\spring_jdbc\libs\mchange-commons-java-0.2.19.jar;E:\IdeaProjects\day6\spring_jdbc\libs\mysql-connector-java-8.0.23.jar;C:\Users\30207\.m2\repository\junit\junit\4.12\junit-4.12.jar;C:\Users\30207\.m2\repository\org\hamcrest\hamcrest-core\1.3\hamcrest-core-1.3.jar" com.intellij.rt.junit.JUnitStarter -ideVersion5 -junit4 domain.JdbcTemplateDemo2
Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.
1月 23, 2021 4:46:20 下午 com.alibaba.druid.pool.DruidDataSource info
信息: {dataSource-1} inited
test6_1:
Emp{id=1001, ename='孙悟空', job_id=4, mgr=1004, joindate=2000-12-17, salary=10000.0, bonus=0.0, dept_id=20}
Emp{id=1002, ename='卢俊义', job_id=3, mgr=1006, joindate=2001-02-20, salary=16000.0, bonus=3000.0, dept_id=30}
Emp{id=1003, ename='林冲', job_id=3, mgr=1006, joindate=2001-02-22, salary=12500.0, bonus=5000.0, dept_id=30}
Emp{id=1004, ename='唐僧', job_id=2, mgr=1009, joindate=2001-04-02, salary=29750.0, bonus=0.0, dept_id=20}
Emp{id=1005, ename='李逵', job_id=4, mgr=1006, joindate=2001-09-28, salary=12500.0, bonus=14000.0, dept_id=30}
Emp{id=1006, ename='宋江', job_id=2, mgr=1009, joindate=2001-05-01, salary=28500.0, bonus=0.0, dept_id=30}
Emp{id=1007, ename='刘备', job_id=2, mgr=1009, joindate=2001-09-01, salary=24500.0, bonus=0.0, dept_id=10}
Emp{id=1008, ename='猪八戒', job_id=4, mgr=1004, joindate=2007-04-19, salary=30000.0, bonus=0.0, dept_id=20}
Emp{id=1009, ename='罗贯中', job_id=1, mgr=0, joindate=2001-11-17, salary=50000.0, bonus=0.0, dept_id=10}
Emp{id=1010, ename='吴用', job_id=3, mgr=1006, joindate=2001-09-08, salary=15000.0, bonus=0.0, dept_id=30}
Emp{id=1011, ename='沙僧', job_id=4, mgr=1004, joindate=2007-05-23, salary=11000.0, bonus=0.0, dept_id=20}
Emp{id=1012, ename='李逵', job_id=4, mgr=1006, joindate=2001-12-03, salary=9500.0, bonus=0.0, dept_id=30}
Emp{id=1013, ename='小ldfv', job_id=4, mgr=1007, joindate=2002-01-23, salary=13000.0, bonus=0.0, dept_id=10}
Emp{id=1014, ename='关羽', job_id=4, mgr=1007, joindate=2002-01-23, salary=13000.0, bonus=0.0, dept_id=10}
1
1
1
test4:
{id=1001, ename=孙悟空, job_id=4, mgr=1004, joindate=2000-12-17, salary=10000.00, bonus=null, dept_id=20}
-----------------------
test5:
{id=1001, ename=孙悟空, job_id=4, mgr=1004, joindate=2000-12-17, salary=10000.00, bonus=null, dept_id=20}
{id=1002, ename=卢俊义, job_id=3, mgr=1006, joindate=2001-02-20, salary=16000.00, bonus=3000.00, dept_id=30}
{id=1003, ename=林冲, job_id=3, mgr=1006, joindate=2001-02-22, salary=12500.00, bonus=5000.00, dept_id=30}
{id=1004, ename=唐僧, job_id=2, mgr=1009, joindate=2001-04-02, salary=29750.00, bonus=null, dept_id=20}
{id=1005, ename=李逵, job_id=4, mgr=1006, joindate=2001-09-28, salary=12500.00, bonus=14000.00, dept_id=30}
{id=1006, ename=宋江, job_id=2, mgr=1009, joindate=2001-05-01, salary=28500.00, bonus=null, dept_id=30}
{id=1007, ename=刘备, job_id=2, mgr=1009, joindate=2001-09-01, salary=24500.00, bonus=null, dept_id=10}
{id=1008, ename=猪八戒, job_id=4, mgr=1004, joindate=2007-04-19, salary=30000.00, bonus=null, dept_id=20}
{id=1009, ename=罗贯中, job_id=1, mgr=null, joindate=2001-11-17, salary=50000.00, bonus=null, dept_id=10}
{id=1010, ename=吴用, job_id=3, mgr=1006, joindate=2001-09-08, salary=15000.00, bonus=0.00, dept_id=30}
{id=1011, ename=沙僧, job_id=4, mgr=1004, joindate=2007-05-23, salary=11000.00, bonus=null, dept_id=20}
{id=1012, ename=李逵, job_id=4, mgr=1006, joindate=2001-12-03, salary=9500.00, bonus=null, dept_id=30}
{id=1014, ename=关羽, job_id=4, mgr=1007, joindate=2002-01-23, salary=13000.00, bonus=null, dept_id=10}
{id=1015, ename=郭靖, job_id=null, mgr=null, joindate=null, salary=null, bonus=null, dept_id=10}
test7:
14
Process finished with exit code 0