1.连接池
1.1为什么要使用连接池
数据库的连接池
现在使用的db.properties这个文件,读取这个文件中的各个属性值,进行数据库的连接,Drivermanager.getConnection()获取数据库的连接对象。
每一次执行业务(增删改查)的时候,重新申请资源(Connection对象)。运行结束以后。直接关闭资源。这样不好,会导致数据库压力过大。好比咱们磁盘,每一次都需要读取,写入。
磁盘是会老化的。新的磁盘。随便读随便写。不会坏,为什么咱们用了八年的话会坏。是因为在这八年内你来回读,来回写。磁盘扛不住的。
数据库连接池:池,游泳池,盛放很多的水
在这个池子中存放很多的连接对象。不用每次都重新连接数据库。
先连数据库,获取数据库的连接对象以后,执行sql语句。执行完sql语句以后要关闭连接对象。但是不是真正的关闭,而是把这个连接对象放到这个池子中,下次再进行sql的操作的时候,直接从池子中取连接对象。而不是重新去连接数据库了。这个池子起到一个缓存的作用!!!连接池的目的保存咱们数据库的。
1.2连接池需要考虑哪些问题【代码的角度】
数据库连接池是用来管理数据库连接资源,属性等
数据库连接必要的参数:1.url 2.user 3.assword 4.数据库的驱动
好比咱们写过一个db.properties这个文件
连接池对于数据库对象的管理:
1.初始化的容量,连接对象的容量
2.容量的最大值,连接对象的最大值
3.等待时间
数据库连接对象是需要归还的,依然是用最原始的close方法,
常见的数据库池:
1.C3P0 2.DBCP 3.Druid(德鲁伊) 阿里的连接池(2020以后也不用了)
1.3C3P0连接池的使用【重点】
使用流程:
1.导包:需要三个包(可以在maven中央仓库里找到)
c3p0-0.9.5.jar
mchage-common-java-0.2.19.jar
mysql-connector-java-5.1.47.jar
2.书写连接池的配置文件 c3p0-config.xml文件(你只需要复制黏贴就可以了,不需要写),这个文件放在src
3.创建核心的类
<?xml version="1.0" encoding="utf-8" ?>
<c3p0-config>
<!-- 命名的配置 -->
<default-config>
<!-- 连接数据库的4项基本参数 -->
//jdbc驱动
<property name="driverClass">com.mysql.jdbc.Driver</property>
//你的数据库的url地址
<property name="jdbcUrl">jdbc:mysql://localhost:3306/javaqwz?useSSL=false</property>
//数据库的用户名
<property name="user">root</property>
//数据库的密码
<property name="password">1234</property>
<!-- 如果池中数据连接不够时一次增长多少个 -->
<property name="acquireIncrement">5</property>
<!-- 初始化连接数 -->
<property name="initialPoolSize">5</property>
<!-- 最小连接数 -->
<property name="minPoolSize">10</property>
<!-- 最大连接数 -->
<property name="maxPoolSize">40</property>
<!-- JDBC的标准参数,用以控制数据源内加载的PrepareStatements数量 -->
<property name="maxStatements">200</property>
<!-- 连接池内单个连接所拥有的最大缓存statements数 -->
<property name="maxStatementsPerConnection">5</property>
</default-config>
<!-- <named-config name="test">-->
<!-- <!– 连接数据库的4项基本参数 –>-->
<!-- <property name="driverClass">com.mysql.jdbc.Driver</property>-->
<!-- <property name="jdbcUrl">jdbc:mysql://localhost:3306/java2109?useSSL=true</property>-->
<!-- <property name="user">root</property>-->
<!-- <property name="password">123456</property>-->
<!-- <!– 如果池中数据连接不够时一次增长多少个 –>-->
<!-- <property name="acquireIncrement">5</property>-->
<!-- <!– 初始化连接数 –>-->
<!-- <property name="initialPoolSize">5</property>-->
<!-- <!– 最小连接数 –>-->
<!-- <property name="minPoolSize">10</property>-->
<!-- <!– 最大连接数 –>-->
<!-- <property name="maxPoolSize">40</property>-->
<!-- <!– JDBC的标准参数,用以控制数据源内加载的PrepareStatements数量 –>-->
<!-- <property name="maxStatements">200</property>-->
<!-- <!– 连接池内单个连接所拥有的最大缓存statements数 –>-->
<!-- <property name="maxStatementsPerConnection">5</property>-->
<!-- </named-config>-->
</c3p0-config>
import com.mchange.v2.c3p0.ComboPooledDataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class Demo1 {
public static void main(String[] args) throws SQLException {
ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource();
Connection connection = comboPooledDataSource.getConnection();
String sql = "insert into test1 values(22,'大王的',56)";
PreparedStatement preparedStatement = connection.prepareStatement(sql);
int i = preparedStatement.executeUpdate();
preparedStatement.close();
connection.close();
System.out.println(i);
}
}

数据插入成功,可以看到比以前省了不少的功夫,那么咱们还可以更直接一点,把获取connection对象封装一下
import com.mchange.v2.c3p0.ComboPooledDataSource;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class JdbcUtil {
private static ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource();
public static Connection getConnection() throws SQLException {
return comboPooledDataSource.getConnection();
}
public static void close(Connection connection) {
close(connection, null, null);
}
//关闭两个资源 connection statement
public static void close(Connection connection, Statement statement) {
close(connection, statement, null);
}
//关闭三个资源的 connection statement resultSet
public static void close(Connection connection, Statement statement, ResultSet resultSet) {
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (statement != null) {
try {
statement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (resultSet != null) {
try {
resultSet.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
测试类Demo2
import com.qwzstu.util.JdbcUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class Demo2 {
public static void main(String[] args) throws SQLException {
Connection connection = JdbcUtil.getConnection();
String sql = "delete from test1 where id = ?";
PreparedStatement preparedStatement = connection.prepareStatement(sql);
preparedStatement.setObject(1,"22");
int i = preparedStatement.executeUpdate();
JdbcUtil.close(connection,preparedStatement);
System.out.println(i);
}
}

1.5druid连接池
德鲁伊连接池是阿里巴巴前两年在用的一个连接池
使用流程
1.导包
druid-1.0.9.jar
2.书写配置文件 druid.properties,在src文件夹下面新建一个druid.properties
3.创建核心类
druid.properties文件
//jdba驱动
driverClassName=com.mysql.jdbc.Driver
//你的数据库的url
url=jdbc:mysql://localhost:3306/javaqwz?useSSL=false
//数据库的用户
username=root
//密码
password=1234
//下面的参数其实还有很多,不过只是获取connection对象,这些就足够了
initialSize=5
maxActive=20
maxWait=2000
测试类:
import com.alibaba.druid.pool.DruidDataSourceFactory;
import javax.sql.DataSource;
import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.util.Properties;
public class Demo1 {
public static void main(String[] args) throws Exception {
Properties properties = new Properties();
properties.load(new FileInputStream("./src/druid.properties"));
DataSource dataSource = DruidDataSourceFactory.createDataSource(properties);
Connection connection = dataSource.getConnection();
String sql = "insert into test1 values (?,?,?)";
PreparedStatement preparedStatement = connection.prepareStatement(sql);
preparedStatement.setObject(1,108);
preparedStatement.setObject(2,"亚索");
preparedStatement.setObject(3,35);
int i = preparedStatement.executeUpdate();
preparedStatement.close();
connection.close();
System.out.println(i);
}
}

1.6封装
接下来将一下druid连接池在封装的JdbcUtil类下面
import com.alibaba.druid.pool.DruidDataSourceFactory;
import javax.sql.DataSource;
import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
public class JdbcUtil {
private static DataSource dataSource;
static {
Properties properties = new Properties();
try {
properties.load(new FileInputStream("./src/druid.properties"));
dataSource = DruidDataSourceFactory.createDataSource(properties);
} catch (Exception e) {
e.printStackTrace();
}
}
public static Connection getConn () throws SQLException {
return dataSource.getConnection();
}
public static void close(Connection connection) {
close(connection, null, null);
}
//关闭两个资源 connection statement
public static void close(Connection connection, Statement statement) {
close(connection, statement, null);
}
//关闭三个资源的 connection statement resultSet
public static void close(Connection connection, Statement statement, ResultSet resultSet) {
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (statement != null) {
try {
statement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (resultSet != null) {
try {
resultSet.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
测试类:
import com.qwzstu.util.JdbcUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class Demo2 {
public static void main(String[] args) throws SQLException {
Connection conn = JdbcUtil.getConn();
PreparedStatement preparedStatement = conn.prepareStatement("delete from test1 where id = 23");
int i = preparedStatement.executeUpdate();
JdbcUtil.close(conn,preparedStatement);
System.out.println(i);
}
}

2.JDBC的事务操作
2.1事务的四大特性【ssm框架的时候会重新讲事务】
原子性 隔离性 一致性 持久性
2.2在JDBC里面事务怎么执行
要在JDBC里执行事务,本质是将多个sql语句包裹在一个事务上面
语法格式:
try{
Connection conn = Drivermanager.getConnetcion();
conn.setAutoCommit(false);//关闭自动提交
//才去写sql语句
insert(),delete().update();
//如果没有错误的话,直接提交
conn.commit();
}catch(Exception e) {
//如果走异常这一步,怎么办?回滚,回到原来的状态就可以了
conn.rollback();
}finally{
conn.setAutoCommit(true);//一个事务结束以后,开启自动提交。默认的
conn.close();
}
模拟转账
李四给张三转300块钱。
至少得两个sql语句
张三这个账户减钱
李四这个账户增钱
新建一个表

import com.qwzstu.util.JdbcUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class Demo3 {
public static void main(String[] args) throws SQLException {
Connection conn = JdbcUtil.getConn();
try {
conn.setAutoCommit(false);
String sql = "update test3 set money = money + ? where name = ?";
PreparedStatement preparedStatement = conn.prepareStatement(sql);
preparedStatement.setObject(1,300);
preparedStatement.setObject(2,"张三");
int i = preparedStatement.executeUpdate();
String sql1 = "update test3 set money = money + ? where name = ?";
PreparedStatement preparedStatement1 = conn.prepareStatement(sql1);
preparedStatement1.setObject(1,-300);
preparedStatement1.setObject(2,"李四");
int i1 = preparedStatement1.executeUpdate();
conn.commit();
}catch (Exception e){
conn.rollback();
}finally {
conn.setAutoCommit(true);
}
}
}
正确的执行结果
在两个语句中间插入一个异常,先把事务处理注释掉

运行之后可以看到张三的钱加上了但是李四的钱没有减少,这就导致了程序的不安全

关闭自动提交,开启事务处理后再次运行,可以看到钱没有变化,保证了安全性


7569




被折叠的 条评论
为什么被折叠?



