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;。我在循环中保留了一些记录器,我们也有审计表。我无法看到处理状态。你能不能帮助我帮助我做错了什么。
答案 0 :(得分:0)
问题是您在迭代preparedStatement.update
时尝试resultset
。
要使其易于使用,请按以下步骤操作:
迭代resultset
并将所有日期复制到list
现在对update
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;较小的方法,以便代码可以更好地维护/读取。