JAVA连接Oracle

来源:互联网 发布:手机版的淘宝助理 编辑:程序博客网 时间:2024/04/27 19:08

public abstract class BusinessBase extends DAOBase {
 /** Log */
 protected static org.apache.log4j.Category log = org.apache.log4j.Category.getInstance("SQL");
 protected static org.apache.log4j.Category logSQL_Exception = org.apache.log4j.Category.getInstance("SQLException");
 Connection conn = null;
 PreparedStatement stmt = null;
 ResultSet rset = null;
 
 public Connection GetConn() throws Exception
 {
  try{
   conn = getConnection();
  }catch(SQLException sqle){
   log.error("DBControlMenu:selectKengen::" +
     getLogMessage("DB_ERR_CONCLOSE"));
   String errmsg = "["+sqle.getErrorCode()+"]"
   + "-" + sqle.getMessage();
   throw new Exception(errmsg);
  }
  return conn;
 }
 
 public void CloseConn(Connection conn,PreparedStatement stmt) throws Exception
 {
  try{
   
   if(stmt != null){
    stmt.close();
   }
   
   if(conn != null){
    release(conn);
   }
  }catch(SQLException sqle){
   log.error("DBControlMenu:selectKengen::" +
     getLogMessage("DB_ERR_CONCLOSE"));
   String errmsg = "["+sqle.getErrorCode()+"]"
   + "-" + sqle.getMessage();
   throw new Exception(errmsg);
  }
 }
 
 public int ExcuteSQL(ArrayList al)
 throws Exception{
  String strSQL="";
  int RowCount=0;
  try{
//   DB接続
   conn = getConnection();
   if(conn == null){
    log.error("InServiceUpdateDB:main::conn is null");
    throw new Exception(getLogMessage("DB_ERR_GETCON"));
   }
   
   conn.setAutoCommit(false);
   for(int i=0;i<al.size();i++)
   {
    strSQL=al.get(i).toString();
    log.fatal("認証用SQL=" + strSQL);
    stmt = conn.prepareStatement(winToUnicode(strSQL));    
    RowCount += stmt.executeUpdate();
    if(stmt != null){
     stmt.close();
    }
   } 
   
   conn.commit();  
   
  }catch(SQLException sqle){
   sqle.printStackTrace();
   try{
    conn.rollback();
   }
   catch(Exception  ex)
   {
//    logSQL_Exception.fatal("SQLException = " +ex);
    throw new Exception(ex);
   }
   throw new Exception(sqle);
  }finally{
   try{
    
    if(stmt != null){
     stmt.close();
    }
    if(rset != null){
     rset.close();
    }
    if(conn != null){
     release(conn);
    }
   }catch(SQLException sqle){
//    logSQL_Exception.fatal("SQLException = " +sqle);
    throw new Exception(sqle);
   }
  }
  return RowCount;
 }
 
 public int ExcuteSQL(String strSQL)
 throws Exception{
  
  
  try{
//   DB接続
   conn = getConnection();
   if(conn == null){
    log.error("InServiceUpdateDB:main::conn is null");
    throw new Exception(getLogMessage("DB_ERR_GETCON"));
   }
   log.fatal("認証用SQL=" + strSQL);
   conn.setAutoCommit(false);
   stmt = conn.prepareStatement(winToUnicode(strSQL));   
   int RowCount  = stmt.executeUpdate();
   conn.commit();
   return RowCount;
   
  }catch(SQLException sqle){
//   String str = sqle.toString();
//   if(str.indexOf("LND_T_HOUZINLOCK")<0)
//   {
//    logSQL_Exception.fatal("SQLException = " +sqle);
//   }
   throw new Exception(sqle);
  }finally{
   try{
    
    if(stmt != null){
     stmt.close();
    }
    if(rset != null){
     rset.close();
    }
    if(conn != null){
     release(conn);
    }
   }catch(SQLException sqle){
//    logSQL_Exception.fatal("SQLException = " +sqle);
    throw new Exception(sqle);
   }
  }
 }
 
 public ArrayList GetList(String strSQL)
 throws Exception{
  
  
  try{
//   DB接続
   conn = getConnection();
   if(conn == null){
    log.error("InServiceUpdateDB:main::conn is null");
    throw new Exception(getLogMessage("DB_ERR_GETCON"));
   }
   log.fatal("認証用SQL=" + strSQL);
   
   stmt = conn.prepareStatement(strSQL);
   rset = stmt.executeQuery();   
   
   
   ResultSetMetaData rsmd = rset.getMetaData();
   int columnCount = rsmd.getColumnCount();
   ArrayList rows = new ArrayList();
   while(rset.next()) {
    HashMap row = new HashMap();
    for (int i = 1; i <= columnCount; i++) {
     String name = rsmd.getColumnName(i);     
     Object obTemp = null;
     obTemp = rset.getObject(i);
     if (rset.getObject(i) instanceof java.lang.String)
     {
      try{
       
       obTemp = unicodeToWin(rset.getObject(i).toString());
      }
      catch(Exception e)
      {
       //e.printStackTrace();
      }
     }
     row.put(name, obTemp == null ? "" : obTemp);
    }
    rows.add(row);
   }
   
   
   return rows;
   
  }catch(SQLException sqle){
//   String str = sqle.toString();
//   if(str.indexOf("LND_T_HOUZINLOCK")<0)
//   {
//    logSQL_Exception.fatal("SQLException = " +sqle);
//   }

   throw new Exception(sqle);
  }finally{
   try{
    
    if(stmt != null){
     stmt.close();
    }
    if(rset != null){
     rset.close();
    }
    if(conn != null){
     release(conn);
    }
   }catch(SQLException sqle){
//    logSQL_Exception.fatal("SQLException = " +sqle);
    throw new Exception(sqle);
   }
  }
 } 

原创粉丝点击