对不起我的愚蠢,但我在这里发生了一些奇怪的事情。我正在为 AS/400 开发 DB2 数据库,并使用 java 中的一些准备好的语句编写一些 SQL。截至目前,我正在提取 49828 条测试记录。当我运行 pstate.executeUpdate() 时,每次运行 executeUpdate() 时它实际上都返回“2”。不知道为什么。
这是方法:
private void loadBenefitsAndDividendsData() {
PreparedStatement pstate;
String benefitTableName = "PFCASBENE";
String dividendTableName = "PFCASDVACM";
String sqlSelect = "SELECT "+ benefitTableName + ".FPLAN, " +
benefitTableName + ".FPSFLG, " + benefitTableName +
".FPLAMT, " + benefitTableName + ".FUNITS, " +
dividendTableName + ".DAPAMT, " + otherSchema + "." +
dailyTableName + ".DBPOLN FROM " + benefitTableName +
" INNER JOIN " + dividendTableName +
" ON " + benefitTableName + ".FPOLNO = " + dividendTableName +
".DAPOLN" +
" INNER JOIN " + otherSchema + "." + dailyTableName +
" ON " + dividendTableName + ".DAPOLN = " +
otherSchema + "." + dailyTableName + ".DBPOLN";
String sqlUpdate = "UPDATE " + otherSchema + "." + dailyTableName +
" SET DBAPLN = ?, DBASTA = ?, DBAFAC = ?, DBAUNI = ? , DBAPUA = ? WHERE DBPOLN = ?";
String policyNumber,dbapln, dbasta;
BigDecimal dbauni,dbafac, dbapua;
try {
pstate = dataDataSource.getConnection().prepareStatement(sqlUpdate);
ResultSet rSet = dataDataSource.getConnection().
prepareStatement(sqlSelect).executeQuery();
int count = 0;
while (rSet.next()) {
policyNumber = rSet.getString("DBPOLN");
dbapln = rSet.getString("FPLAN");
dbasta = rSet.getString("FPSFLG");
dbafac = rSet.getBigDecimal("FPLAMT");
dbauni = rSet.getBigDecimal("FUNITS");
dbapua = rSet.getBigDecimal("DAPAMT");
pstate.setString(1, dbapln);
pstate.setString(2, dbasta);
pstate.setBigDecimal(3, dbafac);
pstate.setBigDecimal(4, dbauni);
pstate.setBigDecimal(5,dbapua);
pstate.setString(6, policyNumber);
count += pstate.executeUpdate();
}
rSet.getStatement().close();
pstate.close();
} catch (SQLException sqlEx) {
logger.error("SQLException was thrown: " + sqlEx);
}
logger.info(getClass().getName() + "Has successfully loaded the PFCASBENE fields into DAILY01.");
}
当我附加调试器并遵循“计数”时,它每次增加 2。当我对表记录进行初始计数时,我得到了正确的数量,即 49828,在更新之后,我得到了 99656,这恰好是两倍,这表明更新语句实际上是在插入行。
String sqlSelect 返回带有 2 个内部连接的 3 个数据表,我得到了正确的结果。
sqlUpdate 只是获取 3 个连接的 ResultSet 并将它们推送到准备好的语句中,该语句使用结果集中的数据更新 49828 条记录。
我不确定为什么要插入两次,而不是简单地更新。谁能在这里指出我正确的方向?
提前致谢,
温馨问候,
乔什