2016-11-07 58 views
-1
public ResultSet validatePayments() { 

    ConnectionPool connPool = null; 
    Connection dbConn = null; 
    Connection dbUpdatConn = null; 
    CallableStatement callableStatement = null; 
    PreparedStatement psmt = null; 
    ResultSet rs = null; 
    CachedRowSetImpl crs = null; 
    Statement stmt = null; 
    List<UpdatePaymentResult> upr = new ArrayList<UpdatePaymentResult>(); 
    String sqlStatement = "{call myschema.mypkg.sp_get_new_payments(?)}"; 
    String updateSql = "UPDATE temp SET stats= ? ,THREAD = ? WHERE P_NUM = ? "; 
    try { 
     Logger.log(Logger.DEBUG, "SQL=" + sqlStatement, 
       "Creating Connection ...."); 
     connPool = ConnectionPool.getConnectionPool(); 
     dbConn = connPool.getConnection(); 
     callableStatement = dbConn.prepareCall(sqlStatement, 
       ResultSet.TYPE_SCROLL_INSENSITIVE, 
       ResultSet.CONCUR_UPDATABLE); 
     callableStatement.registerOutParameter(1, OracleTypes.CURSOR); 
     callableStatement.executeQuery(); 
     rs = (ResultSet) callableStatement.getObject(1); 
     crs = new CachedRowSetImpl(); 
     crs.populate(rs); 

     dbUpdatConn = connPool.getConnection(); 
     psmt = dbUpdatConn.prepareStatement(updateSql); 
     Logger.log(Logger.DEBUG, "SQL=" + "", 
       "prepared statement started.... "); 
     while (rs.next()) { 
      Logger.log(Logger.DEBUG, "SQL=" + "", 
       "inside loop started.... "); 
      UpdatePaymentResult up = new UpdatePaymentResult(); 
      up.setPaymentReqNum(rs.getString("P_NUM")); 

      upr.add(up); 
      for (UpdatePaymentResult updatePaymentResult : upr) { 
       psmt.setString(1, processing); 
       psmt.setString(2, "my payment thread"); 
       psmt.setString(3, updatePaymentResult.getPaymentReqNum()); 
       psmt.executeUpdate(); 
      } 
     } 
     Logger.log(Logger.DEBUG, "SQL=" + "", 
       "prepared statement started.... "); 
    } catch (Exception e) { 
     Logger.log(Logger.RECEIVER, "Error while getting Payment record " 
       + e, "selectNew :"); 

    } finally { 
     try { 
      if (rs != null) 
       rs.close(); 
      if (callableStatement != null) 
       callableStatement.close(); 
      if (psmt != null) 
       psmt.close(); 
      if (connPool != null && dbConn != null) { 
       dbConn.setAutoCommit(true); 
       connPool.returnConnection(dbConn); 
      } 
     } catch (SQLException e) { 
      Logger.log(Logger.Debug, 
        "Error whilst getting Payment record" + e, "hello"); 
     } 
    } 
    return crs; 
} 

大家好,如何選擇和更新結果集?

我想選擇DB值的列表,將結果傳遞到其他類。同時,我想收取付款編號並將狀態更新爲「處理」。一旦我將結果集傳遞給另一個方法,它會將狀態更新爲「已處理」。我在循環中保留了一些記錄器,我們也有審計表。我無法看到處理狀態。你能否請一個人幫我解決我犯的錯誤。

+0

ResultSet並不意味着保持打開並傳遞。將ResultSet複製到支付對象列表中。返回支付對象列表。你的validatePayments方法做了太多事情。把你的方法分解成許多更小的方法。 –

+0

你目前的代碼有什麼問題? –

+0

它的遺留代碼如果我改變調用方法簽名,我需要在如此多的地方做出如此改變。調用方法期望結果集。 – user3647134

回答

0

問題是,當您在迭代resultset時,您正在嘗試執行preparedStatement.update

爲了使它輕鬆地工作,請按照下列步驟操作:

  1. 遍歷resultset和所有日期複製到list第一

  2. 現在在list

每個條目做 update

您可以查看下面的代碼:

public ResultSet validatePayments() { 
    ConnectionPool connPool = null; 
    Connection dbConn = null; 
    Connection dbUpdatConn = null; 

    CallableStatement callableStatement = null; 
    PreparedStatement psmt = null; 
    ResultSet rs = null; 
    CachedRowSetImpl crs = null; 
    Statement stmt = null; 

    List<UpdatePaymentResult> upr = new ArrayList<UpdatePaymentResult>(); 
    String sqlStatement = "{call myschema.mypkg.sp_get_new_payments(?)}"; 
    String updateSql = "UPDATE temp SET stats= ? ,THREAD = ? WHERE P_NUM = ? "; 

    try { 
     Logger.log(Logger.DEBUG, "SQL=" + sqlStatement, 
       "Creating Connection ...."); 
     connPool = ConnectionPool.getConnectionPool(); 
     dbConn = connPool.getConnection(); 
     callableStatement = dbConn.prepareCall(sqlStatement, 
       ResultSet.TYPE_SCROLL_INSENSITIVE, 
       ResultSet.CONCUR_UPDATABLE); 
     callableStatement.registerOutParameter(1, OracleTypes.CURSOR); 
     callableStatement.executeQuery(); 
     rs = (ResultSet) callableStatement.getObject(1); 
     crs = new CachedRowSetImpl(); 
     crs.populate(rs); 

     dbUpdatConn = connPool.getConnection(); 

     Logger.log(Logger.DEBUG, "SQL=" + "", 
       "prepared statement started.... "); 
     while (rs.next()) { 
      Logger.log(Logger.DEBUG, "SQL=" + "", 
       "inside loop started.... "); 
      UpdatePaymentResult up = new UpdatePaymentResult(); 
      up.setPaymentReqNum(rs.getString("P_NUM")); 

      //only add to list here 
      upr.add(up); 
     } 

     //Now update each entry in the list 
     psmt = dbUpdatConn.prepareStatement(updateSql); 
     for (UpdatePaymentResult updatePaymentResult : upr) { 
       psmt.setString(1, processing); 
       psmt.setString(2, "my payment thread"); 
       psmt.setString(3, updatePaymentResult.getPaymentReqNum()); 
       psmt.executeUpdate(); 
     } 

     Logger.log(Logger.DEBUG, "SQL=" + "", 
       "prepared statement started.... "); 
    } catch (Exception e) { 
     Logger.log(Logger.RECEIVER, "Error while getting Payment record " 
       + e, "selectNew :"); 

    } finally { 
     try { 
      if (rs != null) 
       rs.close(); 
      if (callableStatement != null) 
       callableStatement.close(); 
      if (psmt != null) 
       psmt.close(); 
      if (connPool != null && dbConn != null) { 
       dbConn.setAutoCommit(true); 
       connPool.returnConnection(dbConn); 
      } 
     } catch (SQLException e) { 
      Logger.log(Logger.Debug, 
        "Error whilst getting Payment record" + e, "hello"); 
     } 
    } 
    return crs; 
} 

注:這是不以處理同樣的方法兩個結果檢索和更新,而試圖把它們分開到兩個獨立的&更小的方法,這樣的代碼可以更好地維護/可讀的最佳做法。

+0

你有任何關於吹碼的建議嗎? rs =(ResultSet)callableStatement.getObject(1); crs = new CachedRowSetImpl(); crs.populate(rs); – user3647134

+0

你的意思是什麼建議?我沒有明白 – developer

+0

我們有替代方法來做到這一點嗎? – user3647134