-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJdbcDemo10.java
More file actions
57 lines (48 loc) · 1.53 KB
/
Copy pathJdbcDemo10.java
File metadata and controls
57 lines (48 loc) · 1.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package jdbc;
import jdbc.util.JDBCUtils;
import java.sql.SQLException;
import java.sql.*;
/**
* @author : CodeWater
* @create :2022-03-09-10:49
* @Function Description :事务操作
*/
public class JdbcDemo10 {
public static void main(String[] args) {
Connection conn = null;
PreparedStatement pstmt1 = null;
PreparedStatement pstmt2 = null;
try {
conn = JDBCUtils.getConnection();
//开启事务,也就是手动提交,取消自动
conn.setAutoCommit(false);
//张三给李四转500
String sql1 = "update account set balance = balance - ? where id = ?";
String sql2 = "update account set balance = balance + ? where id = ?";
pstmt1 = conn.prepareStatement(sql1);
pstmt2 = conn.prepareStatement(sql2);
pstmt1.setDouble(1, 500);
pstmt1.setInt(2, 1);
pstmt2.setDouble(1, 500);
pstmt2.setInt(2, 2);
pstmt1.executeUpdate();
//制造异常
int i = 3 / 0;
pstmt2.executeUpdate();
conn.commit();
} catch (Exception e) {
//rollback
try {
if (conn != null) {
conn.rollback();
}
} catch (SQLException e1) {
e1.printStackTrace();
}
e.printStackTrace();
} finally {
JDBCUtils.close(pstmt1, conn);
JDBCUtils.close(pstmt2, conn);
}
}
}