@Test
public void Test02() {
Connection conn = null;
try {
conn = getConnection();
conn.setAutoCommit(false);
int aa = state04(conn, "UPDATE user_table SET balance=balance-100 WHERE user=?", "AA");
int i = 10/0;
int bb = state04(conn, "UPDATE user_table SET balance=balance+100 WHERE user=?", "BB");
conn.commit();
System.out.println("交易成功");
} catch (Exception e) {
e.printStackTrace();
try {
conn.rollback();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
} finally {
JDBCUtil.closeResource(conn, null);
}
}
//通用的增删改 --version -2.0 考虑数据库事务的转账操作
public int state04(Connection conn, String sql, Object ...args) throws Exception {
PreparedStatement ps = null;
try {
conn = getConnection();
ps = conn.prepareStatement(sql);
for (int i = 0; i < args.length; i++) {
ps.setObject(i + 1, args[i]);
}
return ps.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
} finally {
JDBCUtil.closeResource(null, ps);
}
return 0;
}
用数据库语句在数据库是可以成功回滚数据的
看的是尚硅谷的视频,找不到问题在哪。