package jdbc_affair; import java.sql.Connection; public class BaseBiz { /** *開始一個事務 * @param con 數據庫的連接。 * @throw* **ception 執行操作時發生的任何異常。 */ public static void beginTransaction(Connection con) throw* **ception { if (con == null) { throw new Exception("Can not start transaction with a null onnection!"); } con.setAutoCommit(false); // 更改JDBC事務的默認提交方式 } /** *開始一個事務 * @param con 數據的連接。 * @param success 是否進行提交的標志,true - 提交,否則回退。 * @throw* **ception 執行操作時發生的任何異常。 */ public static void endTransaction(Connection con, boolean success) throw* **ception { if (con == null) { throw new Exception("Can not end transaction with a null connection!"); } if (success) { con.commit(); con.setAutoCommit(true);// 恢復JDBC事務的默認提交方式 } else { con.rollback(); } } } |