彭老師幫看下我自己封裝了個JDBC的通用查詢,更新函式有沒有什麼問題。

wgmao發表於2006-09-16
函式功能實現通用的增刪改查,這個是第一版,還需要加的功能很多,希望把問題在萌芽階段就找出來。

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">

相關文章