2016-11-07 5 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をPaymentオブジェクトのリストにコピーします。支払オブジェクトの一覧を返します。あなたのvalidatePaymentsメソッドはあまりにも多くのことをしています。メソッドを多くの小さなメソッドに分割します。 –

+0

現在のコードで何が問題になっていますか? –

+0

私はメソッドシグネチャを呼び出す変更する場合、私は非常に多くの場所で変更を加える必要があるそのレガシーコード。呼び出しメソッドは、結果セットを期待しています。 – user3647134

答えて

0

resultsetを反復している間にpreparedStatement.updateを実行しようとしているという問題があります。

それは次の手順を実行し、簡単に動作させるために:resultset以上

  1. 反復と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; 
} 

注:同じ方法で結果の取得と更新の両方を処理するためのベストプラクティスではありません、むしろ、コードが読みやすい/優れたメンテナンス性ことができるように、2つの別々の&小さなメソッドにそれらを分割してみてください。

+0

ブローコードスニペットのご提案はありますか? rs =(ResultSet)callableStatement.getObject(1); crs = new CachedRowSetImpl(); crs.populate(rs); – user3647134

+0

提案があれば、どういう意味ですか?私はそれを得ていない – developer

+0

これを行うには別の方法がありますか? – user3647134

関連する問題