jdbc的executeBatch()返回正确的行计数

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了jdbc的executeBatch()返回正确的行计数相关的知识,希望对你有一定的参考价值。

如果你想让你的JDBC驱动从executeBatch()返回正确的行计数 , 那么将此属性设为true(开启这个选项通常是安全 请问具体怎么实现呢
如果你的sql正常执行的话 都会返回-2 你可懂哈

String sql="sql内容";
int[] i=statement.executeBatch(sql);
//试试看我也不是很懂
将一批命令提交给数据库来执行,如果全部命令执行成功,则返回更新计数组成的数组。返回数组的 int
元素的排序对应于批中的命令,批中的命令根据被添加到批中的顺序排序。方法 executeBatch
返回的数组中的元素可能为以下元素之一:
1.大于等于 0 的数 - 指示成功处理了命令,是给出执行命令所影响数据库中行数的更新计数
2.SUCCESS_NO_INFO 的值 - 指示成功执行了命令,但受影响的行数是未知的
如果批量更新中的命令之一无法正确执行,则此方法抛出 BatchUpdateException,并且 JDBC
驱动程序可能继续处理批处理中的剩余命令,也可能不执行。无论如何,驱动程序的行为必须与特定的 DBMS
一致,要么始终继续处理命令,要么永远不继续处理命令。如果驱动程序在某一次失败后继续进行处理,则
BatchUpdateException.getUpdateCounts
方法返回的数组将包含的元素与批中存在的命令一样多,并且其中至少有一个元素将为:
3.EXECUTE_FAILED 的值 - 指示未能成功执行命令,仅当命令失败后驱动程序继续处理命令时出现
参考技术A 没写明白是什么问题。

executeBatchint[] executeBatch()
throws SQLExceptionSubmits a batch of commands to the database for execution and
if all commands execute successfully, returns an array of update counts.
The int elements of the array that is returned are ordered
to correspond to the commands in the batch, which are ordered
according to the order in which they were added to the batch.
The elements in the array returned by the method executeBatch
may be one of the following:

A number greater than or equal to zero -- indicates that the
command was processed successfully and is an update count giving the
number of rows in the database that were affected by the command's
execution

A value of SUCCESS_NO_INFO -- indicates that the command was
processed successfully but that the number of rows affected is
unknown

If one of the commands in a batch update fails to execute properly,
this method throws a BatchUpdateException, and a JDBC
driver may or may not continue to process the remaining commands in
the batch. However, the driver's behavior must be consistent with a
particular DBMS, either always continuing to process commands or never
continuing to process commands. If the driver continues processing
after a failure, the array returned by the method
BatchUpdateException.getUpdateCounts
will contain as many elements as there are commands in the batch, and
at least one of the elements will be the following:

A value of EXECUTE_FAILED -- indicates that the command failed
to execute successfully and occurs only if a driver continues to
process commands after a command fails

The possible implementations and return values have been modified in
the Java 2 SDK, Standard Edition, version 1.3 to
accommodate the option of continuing to proccess commands in a batch
update after a BatchUpdateException obejct has been thrown.
Returns:an array of update counts containing one element for each
command in the batch. The elements of the array are ordered according
to the order in which commands were added to the batch.
Throws:SQLException - if a database access error occurs,
this method is called on a closed Statement or the
driver does not support batch statements. Throws BatchUpdateException
(a subclass of SQLException) if one of the commands sent to the
database fails to execute properly or attempts to return a result set.Since:1.3See Also:addBatch(java.lang.String),
DatabaseMetaData.supportsBatchUpdates()
参考技术B 求源代码.......look、、、 参考技术C 不用开启,默认就会返回的啊!

以上是关于jdbc的executeBatch()返回正确的行计数的主要内容,如果未能解决你的问题,请参考以下文章

如何使用 SimpleJdbcInsert 和 executeBatch 和 MYSQL JDBC 驱动程序获取生成的密钥?

聊聊 JDBC 的 executeBatch || 对比下不同数据库对 JDBC batch 的实现细节

无法在 Netezza 上执行 executeBatch() - JDBC

JDBC批量插入数据优化,使用addBatch和executeBatch

JDBC批量插入数据优化,使用addBatch和executeBatch

java对mysql进行批处理插入数据,如何返回错误的没插入数据