package com.kuang.lesson04;
import com.kuang.lesson02.utils.JdbcUtils;
import java.sql.*;
public class TestTransaction {
public static void main(String[] args) throws SQLException {
Connection conn =null;
PreparedStatement st= null;
ResultSet rs=null;
try {
conn= JdbcUtils.getConnection();
//关闭数据库的自动提交,自动开启事务
conn.setAutoCommit(false);//开启事务
String sql1 ="update account set money = money-100 where name = 'A'";
st=conn.prepareStatement(sql1);
st.executeUpdate();
String sql2 ="update account set money = money+100 where name = 'B'";
st=conn.prepareStatement(sql2);
st.executeUpdate();
//业务完毕,提交事务
conn.commit();
System.out.println("成功");
} catch (SQLException e) {
try {
conn.rollback();//如果失败就回滚事务
} catch (SQLException ex) {
ex.printStackTrace();
}
e.printStackTrace();
}finally {
JdbcUtils.release(conn,st,rs);
}
}
}