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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19022175/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 13:27:43  来源:igfitidea点击:

ExecuteBatch method return array of value -2 in java

javaoraclejdbc

提问by Deepesh Uniyal

When I am executing executeBatch method in java, its returning an int array that's fine but the value is -2 of all array elements,

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

it should be 0 or +ve number that shows number of rows affected, but its returning -2,

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

when I checked it in database all updates done successful. please explain what is the meaning of this -2 and how I find number of rows affected.

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

Thank, Deepesh Uniyal

谢谢,Deepesh Uniyal

采纳答案by piet.t

The jdbc-spec has the following to say about the return-code of batch-updates:

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

■ 0 或更大 — 命令已成功处理,该值是一个更新计数,指示数据库中受命令执行影响的行数第 14 章批量更新 121

■ Statement.SUCCESS_NO_INFO — 命令已成功处理,但受影响的行数未知

Statement.SUCCESS_NO_INFO is defined as being -2, so your result says everything worked fine, but you won't get information on the number of updated columns.

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

The oracle-documentation states:

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.

? 对于准备好的语句批处理,不可能知道批处理中每个单独语句在数据库中影响的行数。因此,所有数组元素的值都为 -2。根据 JDBC 2.0 规范,值 -2 表示操作成功但受影响的行数未知。

? 对于通用语句批处理,该数组包含实际更新计数,指示受每个操作影响的行数。只有在标准批处理的 Oracle 实现中的通用语句的情况下才能提供实际更新计数。

? 对于可调用语句批处理,无论受每个操作影响的行数如何,服务器始终返回值 1 作为更新计数。

So it seems if you need the update-counts you can't use PreparedStatements but have to fall back to plain Statements.

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

回答by Thirumalai Parthasarathi

A value of -2 indicates that a element was processed successfully, but that the number of effected rows is unknown.

值 -2 表示元素已成功处理,但受影响的行数未知。

specs

眼镜

回答by Florent Guillaume

Note that since Oracle 12c this shouldn't be the case anymore:

请注意,由于 Oracle 12c 不再是这种情况:

For a prepared statement batch, the array contains the actual update counts indicating the number of rows affected by each operation.

对于准备好的语句批处理,该数组包含实际更新计数,指示受每个操作影响的行数。

See https://docs.oracle.com/database/121/JJDBC/oraperf.htm#JJDBC28773

请参阅https://docs.oracle.com/database/121/JJDBC/oraperf.htm#JJDBC28773

回答by mannedear

Standard Update Batching...

标准更新批处理...

conn.setAutoCommit(false);

PreparedStatement pstmt = 
          conn.prepareStatement("INSERT INTO employees VALUES(?, ?)");

pstmt.setInt(1, 2000);
pstmt.setString(2, "Milo Mumford");
pstmt.addBatch();

pstmt.setInt(1, 3000);
pstmt.setString(2, "Sulu Simpson");
pstmt.addBatch();

int[] updateCounts = pstmt.executeBatch();

conn.commit();

pstmt.close();
...

You can process the update counts array to determine if the batch processed successfully.

您可以处理更新计数数组以确定批处理是否成功。

Error Handling in the Oracle Implementation of Standard Batching
If any one of the batched operations fails to complete successfully or attempts to return a result set during an executeBatch call, then the processing stops and a java.sql.BatchUpdateException is generated.

标准批处理的 Oracle 实现中的错误处理
如果任何批处理操作未能成功完成或在 executeBatch 调用期间尝试返回结果集,则处理将停止并生成 java.sql.BatchUpdateException。

After a batch exception, the update counts array can be retrieved using the getUpdateCounts method of the BatchUpdateException object. This returns an int array of update counts, just as the executeBatch method does. In the Oracle implementation of standard update batching, contents of the update counts array are as follows, after a batch is processed:

发生批处理异常后,可以使用 BatchUpdateException 对象的 getUpdateCounts 方法检索更新计数数组。这将返回一个更新计数的 int 数组,就像 executeBatch 方法所做的那样。在标准更新批处理的 Oracle 实现中,更新计数数组的内容在批处理完成后如下:

  • For a prepared statement batch, in case of an error in between execution of the batch, the executeBatch method cannot return a value, instead it throws a BatchUpdateException. In this case, the exception itself carries an int array of size n as its data, where n is the number of successful record executions. For example, if the batch is of size 5 and the error occurs at the 4th record, then the BatchUpdateException has an array of size 3 (3 records executed successfully) and each item in the array represents how many rows were affected by each of them.

  • For a generic statement batch or callable statement batch, the update counts array is only a partial array containing the actual update
    counts up to the point of the error. The actual update counts can be provided because Oracle JDBC cannot use true batching for generic and callable statements in the Oracle implementation of standard update
    batching.

  • 对于准备好的语句批处理,如果批处理执行之间出现错误,executeBatch 方法无法返回值,而是抛出 BatchUpdateException。在这种情况下,异常本身携带一个大小为 n 的 int 数组作为其数据,其中 n 是成功执行的记录数。例如,如果批处理的大小为 5 并且错误发生在第 4 条记录,则 BatchUpdateException 有一个大小为 3 的数组(成功执行了 3 条记录),数组中的每一项表示受它们影响的行数.

  • 对于通用语句批处理或可调用语句批处理,更新计数数组只是包含
    直到错误点的实际更新计数的部分数组。可以提供实际更新计数是因为 Oracle JDBC 无法在标准更新
    批处理的 Oracle 实现中对通用和可调用语句使用真正的批处理。

For example, if there were 20 operations in the batch, the first 13 succeeded, and the 14th generated an exception, then the update counts array will have 13 elements, containing actual update counts of the successful operations.

例如,如果批处理中有 20 个操作,前 13 个成功,第 14 个生成异常,则更新计数数组将有 13 个元素,包含成功操作的实际更新计数。

You can either commit or roll back the successful operations in this situation, as you prefer.

在这种情况下,您可以根据自己的喜好提交或回滚成功的操作。

In your code, upon failed processing of a batch, you should be prepared to handle either -3 or true update counts in the array elements when an exception occurs. For a failed batch processing, you will have either a full array of -3 or a partial array of positive integers.

在您的代码中,当批处理失败时,您应该准备好在发生异常时处理数组元素中的 -3 或 true 更新计数。对于失败的批处理,您将拥有 -3 的完整数组或正整数的部分数组。