gpt4 book ai didi

java - java中的ExecuteBatch方法返回值-2的数组

转载 作者:IT老高 更新时间:2023-10-28 20:39:33 25 4
gpt4 key购买 nike

当我在java中执行executeBatch方法时,它返回一个很好的int数组,但是所有数组元素的值为-2,

它应该是 0 或 +ve 数字,表示受影响的行数,但它返回 -2,

当我在数据库中检查它时,所有更新都成功完成。请解释一下这个 -2 的含义以及我如何找到受影响的行数。

谢谢,Deepesh Uniyal

最佳答案

jdbc-spec 对批量更新的返回码有以下说法:

■ 0 or greater — the command was processed successfully and the value is an update count indicating the number of rows in the database that were affected by the command’s execution Chapter 14 Batch Updates 121

■ Statement.SUCCESS_NO_INFO — the command was processed successfully, but the number of rows affected is unknown

Statement.SUCCESS_NO_INFO 定义为 -2,因此您的结果表明一切正常,但您不会获得有关更新列数的信息。

oracle 文档指出:

• For a prepared statement batch, it is not possible to know the number of rows affected in the database by each individual statement in the batch. Therefore, all array elements have a value of -2. According to the JDBC 2.0 specification, a value of -2 indicates that the operation was successful but the number of rows affected is unknown.

• For a generic statement batch, the array contains the actual update counts indicating the number of rows affected by each operation. The actual update counts can be provided only in the case of generic statements in the Oracle implementation of standard batching.

• For a callable statement batch, the server always returns the value 1 as the update count, irrespective of the number rows affected by each operation.

因此,如果您需要更新计数,您似乎不能使用 PreparedStatements 但必须回退到普通 Statements。

关于java - java中的ExecuteBatch方法返回值-2的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19022175/

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