📄 bean管理事务.java
字号:
public void Transfer(String toId,double amount)throws SQLException{
PreparedStatement pstmt=null;
UserTransaction ut=sessionConttext.getUserTansaction();
try{
pstmt=conn.preparedStatement("UPDATE account SET balance=balance-? WHERE id=?");
pstmt.setDouble(1,amount);
pstmt.setString(2,id);
if(pstmt.executeUpdate()==1){
pstmt.close();
}
else{
pstmt.close();
throw new SQLException("Can't update the database!");
}
}
catch (SQLException sqlEx){
try{
ut.rollback();
}
catch(Exception transEx){
tranEx.printTrace();
}
throw new SQLException("Can't debit from the database!"+sqlEx.toString());
}
try{
pstmt=conn.preparedStatement("UPDATE account SET balance=balance+? WHERE id=?");
pstmt.setDouble(1,amount);
pstmt.setString(2,toId);
if(pstmt.executeUpdate()==1){
pstmt.close();
return;
}
else{
pstmt.close();
throw new SQLException("Can't update the database!(deposit)");
}
}
catch (SQLException sqlEx){
try{
ut.rollback();
}
catch(Exception transEx){
tranEx.printTrace();
}
throw new SQLException("Can't debit from the database!"+sqlEx.toString());
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -