三層框架:
1:servlet層
2:service層
3:dao層
a:實體類domain
b:工具類utils
b:TestTransaction事務與回滾
package com.zcib.utils;
import java.sql.SQLException;
public class TestTransaction {//這裏不能直接拋異常,要回滾
public static void main(String []args) {
String sql1="update bankaccount set account=account-100 where id=2";//轉賬功能
String sql2="update bankaccount set account=account+100 where id=1";
try{
JDBCUtils.beginTranscation();//開啓事務
JDBCUtils.update(sql1);
int i=4;
if(i%2==0){
throw new RuntimeException("D人爲拋的異常!");
}
JDBCUtils.update(sql2);
JDBCUtils.commitTranscation();//提交事務
}//catch(SQLException | ClassNotFoundException e){
catch (Exception e) {//人爲添加異常使用Exception接收處理,軟件工程
//完成,可回滾
try {
JDBCUtils.rollbackTranscation(); //回滾事務
} catch (SQLException e1) {
throw new RuntimeException(e);
}
}
}
}