Python 官方文档:入门教程 => 点击学习
目录一、数据库的事务1.1 事务概述1.2 事务的属性1.3 JDBC事务处理1.4 数据库事务使用的过程1.5 使用数据库事务的好处二、数据库连接池2.1 数据库连接池技术的优点三
事务的ACID(acid)属性
当一个连接对象被创建时,默认情况下是自动提交事务:每次执行一个 sql 语句时,如果执行成功,就会向数据库自动提交,而不能回滚
为了让多个 SQL 语句作为一个事务执行:
1. 调用 Connection 对象的 setAutoCommit(false); 以取消自动提交事务
2.在所有的 SQL 语句都成功执行后,调用 commit(); 方法提交事务
3.在出现异常时,调用 rollback(); 方法回滚事务
4.若此时 Connection 没有被关闭, 则需要恢复其自动提交状态
使用数据库的事务,我们需要配合异常处理try
public void testJDBCTransaction() {
Connection conn = null;
try {
// 1.获取数据库连接
conn = JDBCUtils.getConnection();
// 2.开启事务
conn.setAutoCommit(false);
// 3.进行数据库操作
// 4.若没有异常,则提交事务
conn.commit();
} catch (Exception e) {
e.printStackTrace();
// 5.若有异常,则回滚事务
try {
conn.rollback();
} catch (SQLException e1) {
e1.printStackTrace();
}} finally {
JDBCUtils.close(null, null, conn); } }
使用COMMIT 和 ROLLBACK语句,我们可以:
提交或回滚前的数据状态
提交后的数据状态
说了这么多,还是使用代码来举例说明更加清晰(数据库连接的JDBCUtils类上一章写过了):
package com.company.jdbcDemo;
import com.company.jdbcDemo.JDBCUtils;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class AccountDemo {
public static void main(String[] args) throws SQLException {
//1.获取数据库连接对象
Connection connection = JDBCUtils.getConnection();
PreparedStatement ps = null;
try {
//2.开启事物--禁止自动提交
connection.setAutoCommit(false);
//-------------------------------------------------------------------
//3.做具体的操作---执行sql语句
//预编译
String sql = "update account set balance=? where name=?";
ps = connection.prepareStatement(sql);
//给占位符赋值
ps.setInt(1, 1000);
ps.setString(2, "aa");
//执行sql
ps.executeUpdate();
System.out.println(1 / 0);
//给占位符赋值
ps.setInt(1, 3000);
ps.setString(2, "cc");
//执行sql
ps.executeUpdate();
//-------------------------------------------------------------------
//4.事务提交
connection.commit();
}catch (Exception e){
e.printStackTrace();
//5.事务回滚
connection.rollback();
}finally {
//6.允许自动提交
connection.setAutoCommit(true);
//7.关闭资源----最后关闭资源
JDBCUtils.close(ps,connection);
}
}
}
概述
前面我们的示例代码中,一直在调用我写的那个JDBCUtils类来完成数据库的连接,如果我们在别的工程中,就需要复制一下我的那个类,并且我写的那个也不是很严谨,我前面实现的数据库连接方式存在以下问题:
使用数据库连接池
阿里德鲁伊连接池技术首先分为两步:
1.加入jar包
例如:druid-1.1.10.jar
2.代码步骤
第一步:建立一个数据库连接池
第二步:设置连接池的参数
第三步:获取连接
使用德鲁伊连接数据库的方式一
//1、创建数据源(数据库连接池)对象
DruidDataSource ds =new DruidDataSource();
//2、设置参数
//(1)设置基本参数
ds.setDriverClassName("com.Mysql.jdbc.Driver");
ds.setUrl("jdbc:mysql://localhost:3306/test");
ds.setUsername("root");
ds.setPassword("mysql123");
//3、获取连接
Connection conn = ds.getConnection();
//如果这里没有关闭,就相当于没有还
conn.close();
使用德鲁伊连接数据库的方式二
// 创建配置文件druid.properties
url=jdbc:mysql://localhost:3306/0319db ?rewriteBatchedStatements=true
username=root
passWord=123456
driverClassName=com.mysql.jdbc.Driver
代码如下:
Properties pro = new Properties();
pro.load(TestDruid2.class.getClassLoader().getResourceAsStream("druid.properties"));
DataSource ds=
DruidDataSourceFactory.createDataSource(pro);
Connection conn = ds.getConnection();
这里注意了,德鲁伊配置文件中的key,必须跟我下面一样,否则连接不成功哦
// druid.properties内容
url=jdbc:mysql://localhost:3306/demo
username=root
password=123321
driverClassName=com.mysql.jdbc.Driver
我就直接上操作了,这里还是使用我前面实现的那个JDBCUtils类哈,偷个懒…
package com.company.jdbc2;
import com.company.jdbc.JDBCUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.junit.Test;
import java.sql.SQLException;
import java.util.List;
public class DBUtilsDemo {
@Test
public void test() throws SQLException {
//1.创建操作对象
QueryRunner queryRunner = new QueryRunner();
//2.增,删,改是一个方法
String sql = "insert into student(sid,sname,sage) values(?,?,?)";
//返回值 :有几条数据受到影响
int i = queryRunner.update(JDBCUtils.getConnection(),
sql, 10, "kongkong", 18);
System.out.println("有" + i + "条数据受到影响");
}
@Test
public void test2() throws SQLException {
QueryRunner queryRunner = new QueryRunner();
String sql = "select sid a,sname,sage from student where sid=?";
//注意:类中的属性名一定要和字段名相同。如果不相同则需要在sql语句中使用别名
// Student student = queryRunner.query(JDBCUtils.getConnection(), sql,
// new BeanHandler<Student>(Student.class), 10);
sql = "select sid a,sname,sage from student";
List<Student> list = queryRunner.query(JDBCUtils.getConnection(), sql,
new BeanListHandler<Student>(Student.class));
for (Student s : list) {
System.out.println(s);
}
}
}
使用批处理
当我们需要对进行大批量的数据操作时,可以采用批处理技术,很简单,在url中添加批处理的参数
jdbc:mysql://localhost:3306/Demo?rewriteBatchedStatements=true
示例代码,(咳咳,依旧是我那个JDBCUtils实现连接的工具类…)
package com.company.jdbc3;
import com.company.jdbc.JDBCUtils;
import org.junit.Test;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class BatchDemo {
@Test
public void test2() throws SQLException {
//1.获取数据库连接
Connection connection = JDBCUtils.getConnection();
//2.预编译
PreparedStatement ps = connection.prepareStatement(
"insert into student(sid,sname,sage) values(?,?,?)");
//3.给占位符赋值
for (int i = 1; i <= 100000 ; i++) {
ps.setInt(1,i);
ps.setString(2,"aaa"+i);
ps.setInt(3,i);
//添加到批处理中
ps.addBatch();
if (i % 1000 == 0){
//执行sql
ps.executeBatch();//执行批处理
//清空批处理
ps.clearBatch();
}
}
//4.关资源
JDBCUtils.close(ps,connection);
}
@Test
public void test() throws SQLException {
//1.获取数据库连接
Connection connection = JDBCUtils.getConnection();
//2.预编译
PreparedStatement ps = connection.prepareStatement(
"insert into student(sid,sname,sage) values(?,?,?)");
//3.给占位符赋值
for (int i = 1; i <= 100000 ; i++) {
ps.setInt(1,i);
ps.setString(2,"aaa"+i);
ps.setInt(3,i);
//执行sql
ps.executeUpdate();
}
//4.关资源
JDBCUtils.close(ps,connection);
}
}
到此这篇关于JDBC的扩展知识点总结的文章就介绍到这了,更多相关JDBC知识点内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!
--结束END--
本文标题: JDBC的扩展知识点总结
本文链接: https://www.lsjlt.com/news/125444.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0