gpt4 book ai didi

java - JDBC preparedStatement.executeBatch() 的终止条件

转载 作者:行者123 更新时间:2023-12-01 06:09:39 24 4
gpt4 key购买 nike

我正在使用java.sql库来批量执行更新。当执行preparedStatement.executeBatch()时,理想情况下它应该返回一个更新计数数组。

但在某些情况下,它返回 -2,而不是更新计数,如下所示。

 /**
* The constant indicating that a batch statement executed successfully
* but that no count of the number of rows it affected is available.
*
* @since 1.4
*/
int SUCCESS_NO_INFO = -2;

因此,在这种情况下,当我批量运行查询时,无法确定特定批量查询执行是否更新了任何记录。因此无法确定退出批处理执行循环的终止条件。如果有人有解决方法或建议。这会很有帮助。

我现在有以下情况。

  private static String batchUpdate() throws SQLException {

Connection dbConnection = null;
PreparedStatement preparedStatement = null;
String result = null;

String updateSQL = "update TABLE_NAME set SOME_FLAG ='T' \n" +
" where SOME_USER!='SOMEUSER' and SOME_FLAG = 'F' and rownum<=?";

try {
dbConnection = getDBConnection();
preparedStatement = dbConnection.prepareStatement(updateSQL);

dbConnection.setAutoCommit(false);

while (true) {
preparedStatement.setInt(1, 10000);
preparedStatement.addBatch();
int[] updateResults = preparedStatement.executeBatch();
if (updateResults == null || updateResults.length == 0 || updateResults[0] == -3) {
break;
}
dbConnection.commit();
}

result = "Records are updated!";
} catch (SQLException e) {
result = e.getMessage();
dbConnection.rollback();
} finally {
if (preparedStatement != null) {
preparedStatement.close();
}
if (dbConnection != null) {
dbConnection.close();
}
}
return result;
}

最佳答案

Oracle 11g does not return update counts对于批处理方法。相反,它返回 SUCCESS_NO_INFO。

关于java - JDBC preparedStatement.executeBatch() 的终止条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37449284/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com