?? dbtrans.java
字號:
package com.laoer.bbscs.db;import java.sql.*;import java.util.StringTokenizer;/** * 該類用于數據事務處理 * Title: BBS-CS * Description: BBS-CS(BBS式虛擬社區系統) * Copyright: Copyright (c) 2002 * Company: loveroom.com.cn * @author laoer * @version 3.0 */public class DbTrans { public static Connection conn; Statement stmt; boolean isAutoCommit; /** * 構造函數 */ public DbTrans(){ initConnection(); } /** * 帶參數的構造函數 * @param conn 連接 */ public DbTrans(Connection conn){ this.conn = conn; } /** * 初始化建立連接 */ private void initConnection(){ try{ if(conn == null){ DBConnectionManager connMgr=DBConnectionManager.getInstance(); conn = connMgr.getConnection("mysql"); } } catch(Exception ex){ System.out.println("Can not get new Connection"+ex.getMessage()); } } public PreparedStatement getPreparedStmt(String sql) throws SQLException{ PreparedStatement preStmt=null; try { preStmt = conn.prepareStatement(sql); } catch(SQLException ex){ ex.printStackTrace(); throw ex; } return preStmt; } /** * 過程開始 * @throws SQLException 捕捉錯誤 */ public void beginTrans() throws SQLException { try { isAutoCommit = conn.getAutoCommit(); conn.setAutoCommit(false); } catch(SQLException ex) { ex.printStackTrace(); System.out.print("beginTrans Errors"); throw ex; } } /** * 數據事務提交 * @throws SQLException 捕捉錯誤 */ public void commit() throws SQLException { try{ conn.commit(); conn.setAutoCommit(isAutoCommit); } catch(SQLException ex) { ex.printStackTrace(); System.out.print("Commit Errors!"); throw ex; } } /** * 數據事務回滾 */ public void roolback() { try { conn.rollback(); conn.setAutoCommit(isAutoCommit); } catch(SQLException ex) { ex.printStackTrace(); System.out.print("Roolback Error!"); } } /** * 判斷是否為自動加入數據模式 * @return boolean值 * @throws SQLException 捕捉錯誤 */ public boolean getAutoCommit() throws SQLException { boolean result = false; try { result = conn.getAutoCommit(); } catch(SQLException ex) { ex.printStackTrace(); System.out.println("getAutoCommit fail "+ex.getMessage()); throw ex; } return result; } /** * executeQuery操作,用于數據查詢,主要是Select * @param sql 查詢字段 * @return 數據集 * @throws SQLException 捕捉錯誤 */ public ResultSet executeQuery(String sql) throws SQLException { ResultSet rs = null; try { stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY); rs = stmt.executeQuery(sql); } catch (SQLException ex) { ex.printStackTrace(); System.out.println("dbTrans.executeQuery:"+ex.getMessage()); throw ex; } return rs; } /** * executeUpdate操作,用于數據更新,主要是Update,Insert * @param sql 查詢字段 * @throws SQLException 捕捉錯誤 */ public void executeUpdate(String sql) throws SQLException { try { stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE); stmt.executeUpdate(sql); } catch (SQLException ex) { ex.printStackTrace(); System.out.println("dbTrans.executeUpdate:"+ex.getMessage()); throw ex; } } public int[] doBatch(String sql) throws SQLException { int[] rowResult=null; String a; try{ stmt = conn.createStatement(); StringTokenizer st=new StringTokenizer(sql,";"); while (st.hasMoreElements()) { a = st.nextToken(); stmt.addBatch(a); } rowResult=stmt.executeBatch(); } catch (SQLException ex) { ex.printStackTrace(); System.out.println("dbTrans.doBatch"+ex.getMessage()); throw ex; } return rowResult; } /** * 關閉對象 * @throws SQLException 捕捉錯誤 */ public void close() throws SQLException{ if(conn != null) conn.close(); //if(rset != null) rset.close(); if(stmt != null) stmt.close(); } /* public void close() throws SQLException { try { stmt.close(); stmt = null; conn.close(); conn = null; } catch (SQLException ex) { ex.printStackTrace(); System.out.println("Closeing connection fail"+ex.getMessage()); throw ex; } } */ /** * 收尾和垃圾收集 * @throws Throwable 捕捉錯誤 */ //protected void finalize() throws Throwable{ // close(); //}}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -