【JAVA】事务回滚与网页跳转
代码
Connection conn=null;
PreparedStatement pstmt1=null;
PreparedStatement pstmt2=null;
try{
//获取连接
conn=JDBCUtils.getConnection();
//开启事务
conn.setAutoCommit(false);
//定义sql
String sql1=“update account set balance = balance - ? where id= ?”;
String sql2=“update account set balance = balance + ? where id= ?”;
//获取执行sql对象
pstmt1=conn.prepareStatement(sql1);
pstmt2=conn.prepareStatement(sql2);
//设置参数
pstmt1.setDouble(1,500);
pstmt1.setInt(2,1);
pstmt2.setDouble(1,500);
pstmt2.setInt(2,2);
//执行sql
pstmt1.executeUpdate();
//手动制造一个异常
// int i=3/0;
pstmt2.executeUpdate();
//提交事务
conn.commit();
} catch (Exception e) {
//事务回滚
try{
if(conn!=null){
<