如何选择和更新结果集?

时间:2016-11-07 13:23:31

标签: java jdbc

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中选择值列表,并将结果传递给另一个类。同时我想收取付款编号并将状态更新为&#34;处理&#34;。一旦我将结果集传递给另一个方法,它就会将状态更新为&#34;已处理&#34;。我在循环中保留了一些记录器,我们也有审计表。我无法看到处理状态。你能不能帮助我帮助我做错了什么。

1 个答案:

答案 0 :(得分:0)

问题是您在迭代preparedStatement.update时尝试resultset

要使其易于使用,请按以下步骤操作:

  1. 迭代resultset并将所有日期复制到list

  2. 现在对update

  3. 中的每个条目list进行操作

    您可以查看以下代码:

    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;
    }
    

    注意:在同一方法中处理结果检索和更新不是最佳做法,而是尝试将它们分成两个单独的&amp;较小的方法,以便代码可以更好地维护/读取。