转账案例代码实现
@Test
/**
* 完成转账的案例
*/
public void demo1(){
Connection conn = null;
PreparedStatement pstmt = null;
try{
/**
* 完成转账代码:
* * 扣除某个账号的钱
* * 给另外一个账号加钱
*/
// 获得连接:
conn = JDBCUtils.getConnection();
// 编写SQL语句:
String sql = "update account set money = money + ? where name = ?";
// 预编译SQL:
pstmt = conn.prepareStatement(sql);
// 设置参数:
// 用aaa账号给bbb账号转1000元
pstmt.setDouble(1, -1000);
pstmt.setString(2, "aaa");
// 执行SQL:扣除aaa账号1000元
pstmt.executeUpdate();
// int i = 1 / 0;
// 给bbb账号加1000
pstmt.setDouble(1, 1000);
pstmt.setString(2, "bbb");
pstmt.executeUpdate();
}catch(Exception e){
e.printStackTrace();
}finally{
JDBCUtils.release(pstmt, conn);
}
}
在转账中没有添加事务的管理,出现aaa账号的钱被转丢了,但是bbb账号的钱没有任何变化。需要给转账的功能添加事务的管理。
1.1.1 在转账中添加事务管理:1.1.1.1 事务管理的API:
1.1.1.2 在转账中添加事务管理
@Test
/**
* 完成转账的案例
*/
public void demo1(){
Connection conn = null;
PreparedStatement pstmt = null;
try{
/**
* 完成转账代码:
* * 扣除某个账号的钱
* * 给另外一个账号加钱
*/
// 获得连接:
conn = JDBCUtils.getConnection();
// 开启事务
conn.setAutoCommit(false);
// 编写SQL语句:
String sql = "update account set money = money + ? where name = ?";
// 预编译SQL:
pstmt = conn.prepareStatement(sql);
// 设置参数:
// 用aaa账号给bbb账号转1000元
pstmt.setDouble(1, -1000);
pstmt.setString(2, "aaa");
// 执行SQL:扣除aaa账号1000元
pstmt.executeUpdate();
int i = 1 / 0;
// 给bbb账号加1000
pstmt.setDouble(1, 1000);
pstmt.setString(2, "bbb");
pstmt.executeUpdate();
// 提交事务:
conn.commit();
}catch(Exception e){
// 回滚事务:
try {
conn.rollback();
} catch (SQLException e1) {
e1.printStackTrace();
}
e.printStackTrace();
}finally{
JDBCUtils.release(pstmt, conn);
}
}