Java 验证spring JDBC批量更新成功

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19225720/
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 15:14:03  来源:igfitidea点击:

Verifying success for spring JDBC batch update

javajdbcspring-jdbcbatch-updates

提问by Ayushi

I am using Spring JDBCTemplate batchUpdate to insert data in a batch. I want to verify if the data is successfully inserted. JDBCTemplate batchUpdate returns an int[][], so which is the right way to verify that the data is inserted?

我正在使用 Spring JDBCTemplate batchUpdate 批量插入数据。我想验证数据是否插入成功。JDBCTemplate batchUpdate 返回一个int[][],那么验证数据插入的正确方法是什么?

This linksays "All batch update methods return an int array containing the number of affected rows for each batch entry. This count is reported by the JDBC driver and it's not always available in which case the JDBC driver simply returns a -2 value". I am unable to understand the significance of returning -2 value here. Does it means the insert was unsuccessful?

链接表示“所有批处理更新方法都返回一个包含每个批处理条目受影响行数的 int 数组。此计数由 JDBC 驱动程序报告,在这种情况下,JDBC 驱动程序仅返回 -2 值并不总是可用”。我无法理解在这里返回 -2 值的重要性。这是否意味着插入不成功?

采纳答案by aviad

-2 does not necessarily mean error, it might be as mentioned, the case of count of affected rows is not available.

-2 并不一定意味着错误,可能是如前所述,受影响行数的情况不可用。

EDIT

编辑

-2 is the value of Statement.SUCCESS_NO_INFO (while EXECUTE_FAILED is -3). So unless the driver does not comply with the JDBC specification, -2 unequivocally means success

-2 是 Statement.SUCCESS_NO_INFO 的值(而 EXECUTE_FAILED 是 -3)。因此,除非驱动程序不符合 JDBC 规范,否则 -2 明确表示成功

END OF EDIT

编辑结束

The errors are reported via BatchUpdateException

通过BatchUpdateException报告错误

Normally, if you run N queries in your batch script you will get the count of updates per query i in the result:

通常,如果您在批处理脚本中运行 N 个查询,您将在结果中获得每个查询 i 的更新计数:

int result[] = jdbcTemplate.batchUpdate(sql);

so:

所以:

result[0]

will hold the update count for the first query,

将保存第一个查询的更新计数,

result[1]

will hold the update count for the second query etc.

将保存第二个查询的更新计数等。