JDBC的复习(四):JDBC的事务
事务的知识
-
JDBC的事务机制:
-
JDBC中的事务是自动提交的!什么是自动提交?
只要执行一条DML语句,则自动提交一次,这是JDBC的默认行为!
但是在实际的业务中,通常是N条语句联合才能完成的,所以必须保证他们这些DML语句在同一个事务中同时成功或者同时失败。
-
验证自动提交。
执行一条sql语句,就提交一次申请!
package com.bjpowernode.jdb;
import java.sql.*;
import java.util.Scanner;
import static java.lang.System.exit;
/**
* @author 31200
*/
public class JdbcTest09 {
public static void main(String[] args) {
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
//1. 注册驱动
Class.forName("com.mysql.jdbc.Driver");
//2. 获取连接
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/bjpowernode?characterEncoding=UTF8&useSSL=false","root","001204");
//3. 获取数据库操作对象
String sql = "update dept set dname = ? where deptno = ?";
ps = conn.prepareStatement(sql);
ps.setString(1,"X公司");
ps.setInt(2,30);
int count = ps.executeUpdate();
System.out.println(count);
// 重新选定占位符号
ps.setString(1,"Y公司");
ps.setInt(2,20);
count = ps.executeUpdate();
System.out.println(count);
//4. 执行sql语句
//5. 遍历结果集
} catch (Exception e) {
e.printStackTrace();
}finally {
//释放资源
try {
if(rs!=null){
rs.close();
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}
try {
if(ps!=null){
ps.close();
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}
try {
if(conn!=null){
conn.close();
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
}
执行成功!!!
账户转账演示事务
- 创建数据库表
-
代码展示
如果存在异常,那么会报错!!
//1. 注册驱动
Class.forName("com.mysql.jdbc.Driver");
//2. 获取连接
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/bjpowernode?characterEncoding=UTF8&useSSL=false","root","001204");
// 将自动提交机制转换为手动提交
// 开启事务
conn.setAutoCommit(false);
//3. 获取数据库操作对象balance
String sql = "update t_act set balance = ? where actno = ?";
ps = conn.prepareStatement(sql);
//传值
ps.setDouble(1,10000);
ps.setInt(2,111);
int count = ps.executeUpdate();
//自定义一个异常
String s = null;
s.toString();
// 传值
ps.setDouble(1,10000);
ps.setInt(2,222);
count += ps.executeUpdate();
System.out.println(count == 2? "转账成功":"转账失败");
//程序能够走到这里,说明程序没有异常,事务结束,手动提交数据
//提交事务
conn.commit();
但是数据库的数据不会改变!
将异常去掉,数据库数据更新成功!!!
package com.bjpowernode.jdb;
import java.sql.*;
/**
* @author 31200
*
* SQL脚本
* drop table if exists t_act;
* create table t_act(
* actno int,
* balance double(7,2)
* );
* insert into t_act(actno,balance) values(111,20000);
* insert into t_act(actno,balance) values(222,0);
* commit;
* select * from t_act;
*/
public class JdbcTest10 {
public static void main(String[] args) {
Connection conn = null;
PreparedStatement ps = null;
try {
//1. 注册驱动
Class.forName("com.mysql.jdbc.Driver");
//2. 获取连接
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/bjpowernode?characterEncoding=UTF8&useSSL=false","root","001204");
// 将自动提交机制转换为手动提交
// 开启事务
conn.setAutoCommit(false);
//3. 获取数据库操作对象balance
String sql = "update t_act set balance = ? where actno = ?";
ps = conn.prepareStatement(sql);
//传值
ps.setDouble(1,10000);
ps.setInt(2,111);
int count = ps.executeUpdate();
// //自定义一个异常
// String s = null;
// s.toString();
// 传值
ps.setDouble(1,10000);
ps.setInt(2,222);
count += ps.executeUpdate();
System.out.println(count == 2? "转账成功":"转账失败");
//程序能够走到这里,说明程序没有异常,事务结束,手动提交数据
//提交事务
conn.commit();
} catch (Exception e) {
if(conn!=null){
try {
conn.rollback();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
e.printStackTrace();
}finally {
//释放资源
try {
if(ps!=null){
ps.close();
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}
try {
if(conn!=null){
conn.close();
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
}
数据被更改!