彭老師幫看下我自己封裝了個JDBC的通用查詢,更新函式有沒有什麼問題。
函式功能實現通用的增刪改查,這個是第一版,還需要加的功能很多,希望把問題在萌芽階段就找出來。
public class DBJdbcRunners { private static Log log = LogFactory.getLog(DBJdbcRunners.class); private boolean autoCommit; Connection connection = null; public DBJdbcRunners()throws SQLException{ autoCommit = true; try { connection = DBUtils.getConnection(); } catch (SQLException e) { throw new SQLException(); } } public void begintx()throws SQLException{ autoCommit = false; if (connection != null){ try { connection.setAutoCommit(false); } catch (SQLException e) { throw new SQLException(); } } } public void committx()throws SQLException{ autoCommit = false; if (connection != null){ try { connection.commit(); connection.setAutoCommit(true); autoCommit = true; } catch (SQLException e) { log.equals("committx exception is ->["+e+"]"); throw new SQLException(); }finally{ DBUtils.closeConnection(connection); } } } public int executeUpdate (String sql) throws SQLException, InputIllegalException { Statement statement = null; try { statement = connection.createStatement(); int i = statement.executeUpdate(sql); return i; }catch( SQLException e){ log.error("error sql is-->["+sql+"]"); log.error(e); if(!autoCommit){ try{ connection.rollback(); }catch(SQLException ex){ log.error(ex); } } throw new SQLException(); } finally{ DBUtils.closeStatement(statement); if(autoCommit){ DBUtils.closeConnection(connection); } } } public void executeQuery ( String sql,boolean isgetTotalNum) throws SQLException,InputIllegalException{ Statement statement = null; PreparedStatement p_statement = null; ResultSet rs = null; if(sql == null){ throw new InputIllegalException("in executeQuery function" + "sql is null"); } try{ if(isgetTotalNum){ statement = connection.prepareStatement(sql,ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); }else{ statement = connection.createStatement(); } rs = statement.executeQuery(sql); if(isgetTotalNum){ rs.last(); log.debug("total row num is-->["+rs.getRow()+"]"); rs.first(); } ResultSetMetaData rsmd = rs.getMetaData(); int numCols = rsmd.getColumnCount(); log.debug("column num is-->["+rsmd.getColumnCount()+"]"); while(rs.next()){ for(int i=1;i<=numCols;i++){ System.out.print("column "+i+" is-->["+rs.getString(i)+"]"); } System.out.print("\n"); } }catch( SQLException e){ log.error("error sql is-->["+sql+"]"); log.error("[ex in executeQuery function]-->["+e+"]"); if(!autoCommit){ try{ if(log.isDebugEnabled()){ log.debug("[in executeQuery function rollback]"); } connection.rollback(); }catch(SQLException ex){ log.error(ex); } } throw new SQLException(); } finally{ DBUtils.closeResultSet(rs); DBUtils.closeStatement(statement); if(autoCommit){ DBUtils.closeConnection(connection); } } } } <p class="indent"> |
相關文章
- 誰能幫我看下哪裡有問題?
- 彭老師,急!!!!!!!!!我的jndi連線池有問題.能不能幫一下我啊.
- 請教彭老師關於jdbc的問題,謝謝!JDBC
- 彭老師有一個問題一直困擾著我,找了很久都解決不了問題
- 新組裝臺式裝完系統後 File:\Boot\BCD 有沒有懂的朋友幫忙看下boot
- 彭老師:您好!請教個在jdonframework中關於查詢list中 KEY的問題???Framework
- swift沒有了BlocksKit,我忍不住自己寫了個ClosuresKitSwiftBloC
- 我自己的Javascript 庫,封裝了一些常用函式JavaScript封裝函式
- 大神幫我看個C函式的問題函式
- 曾經有個能幫我修電腦的程式設計師,我沒有好好珍惜……程式設計師
- 自己做的一個簡歷網頁,有很多bug解決不了,有沒有大神幫我看看網頁
- 一個使用JDBC按Date查詢查詢的問題JDBC
- JSONP 通用函式封裝JSON函式封裝
- 用過javamail的朋友幫幫忙,我真的沒有辦法了?!!!JavaAI
- JBuiler 建立資料庫查詢工程過程中問題,有誰能幫我?UI資料庫
- 為什麼IT培訓機構的老師自己不去工作?沒有在公司繼續做技術?
- 彭老師:您好!關於JF中資料查詢更新問題/分頁問題,麻煩您在百忙中指點迷津。多謝!
- 請問一個有關jdbc效能的問題JDBC
- 請教彭老師:開發工具使用的問題
- 錄屏為什麼沒有聲音?解決這個問題可簡單了!
- 分析競價推廣為什麼有訪問沒有諮詢KMK
- IDC商怎麼搭建一個自己的線上網站,有沒有什麼好的工具網站
- 我是設計師面試官,你有什麼想問我的?面試
- 剛剛有個群友問我,為什麼我的子執行緒更新了 UI 沒報錯?藉此,糾正一些Android 程式設計師的一個知識誤區執行緒UIAndroid程式設計師
- 為什麼中國人沒有自己的程式語言?
- 有沒有通用的SSL證書
- 為什麼 Python 沒有函式過載?如何用裝飾器實現函式過載?Python函式
- 好久沒有更新日誌了
- 有個很有趣的問題,當實體中不是以聚合而是以查詢的方式會出現什麼讓我們忽視的問題。
- 靜態成員函式-this指標沒有指的是沒有給,而不是不能擁有,可以自己給呀函式指標
- 初學者疑惑:C語言中,函式反覆呼叫會有什麼問題?C語言函式
- 面試官,我會寫二分查詢法了!對,沒有 bug 的那種!面試
- 請彭老師指點一下有關Jive論壇安裝除錯方面setup檔案中遇到的問題?除錯
- 為什麼react-redux沒有更新hooks APIReactReduxHookAPI
- 請問彭老師:Jdon所需類庫
- 查詢沒有索引的外來鍵索引
- 安卓7.0安裝app產生android.os.FileUriExposedException異常(都檢查檢查你的自動更新有沒有問題吧)安卓APPAndroidException
- 彭老師請進一個技術以外的問題請談談您的看法