How to get generated keys from JDBC batch insert in Oracle?

旧街凉风 提交于 2019-11-28 23:44:14

It appears that Oracle 12c does not support combining auto-generated keys with batch update according to the following page:

http://docs.oracle.com/cd/E16655_01/java.121/e17657/jdbcvers.htm

See the subsection labeled "Limitations" under the section "Retrieval of Auto-Generated Keys"

Mark Rotteveel

The JDBC 4.1 specification, section 13.6 Retrieving Auto Generated Values says:

It is implementation-defined as to whether getGeneratedKeys will return generated values after invoking the executeBatch method.

So you will need to check if your driver actually supports it for batch updates. As indicated in the answer by Philip O., retrieval of generated keys is not supported with batch updates as documented in Oracle 12 JDBC Standards Support:

You cannot combine auto-generated keys with batch update.

In any case if it is supported by your driver than your statement prepare should be changed to the code below to instruct the driver to retrieve generated keys:

ps = con.prepareStatement(insert, Statement.RETURN_GENERATED_KEYS);

Note: you may need to use one of the other generated keys prepare methods (prepareStatement(sql, columnIndexes) or prepareStatement(sql, columnNames)) as Oracle will return the ROW_ID with the method in my example.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!