简体   繁体   English

如何从Oracle中的JDBC批量插入中获取生成的密钥?

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

I am inserting many records using JDBC batch inserts. 我使用JDBC批量插入插入许多记录。 Is there any way to get the generated key for each record? 有没有办法获得每条记录的生成密钥? Can I use ps.getGeneratedKeys() with batch inserts? 我可以在批量插入中使用ps.getGeneratedKeys()吗?

I am using oracle.jdbc.OracleDriver 我正在使用oracle.jdbc.OracleDriver

final String insert = "Insert into Student(RollNumber, Name, Age) values(StudentSEQ.nextval, ? , ?)";
final int BATCH_SIZE = 998;
int count = 0;
Connection con = null;
PreparedStatement ps =  null;
try {
    con = getConnection();
    ps = con.prepareStatement(insert);
    for (Student s : students) {
        ps.setString(1, s.getName());
        ps.setInt(2, s.getAge());
        ps.addBatch();
        count++;
        if (count % BATCH_SIZE == 0) {
        // Insert records in batches
            ps.executeBatch();
        }
    }
    // Insert remaining records
    ps.executeBatch();
} finally {
    if(ps != null)
        ps.close();
    release(con);
}

I am thinking of using ps.executeUpdate() along with ps.getGeneratedKeys() inside the loop to get the desired result. 我想在循环中使用ps.executeUpdate()ps.getGeneratedKeys()来获得所需的结果。 Any other solutions? 还有其他方法吗?

The JDBC 4.1 specification , section 13.6 Retrieving Auto Generated Values says: JDBC 4.1规范的第13.6节“ 检索自动生成的值”说:

It is implementation-defined as to whether getGeneratedKeys will return generated values after invoking the executeBatch method. 实现定义了getGeneratedKeys是否在调用executeBatch方法后返回生成的值。

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 : 正如Philip O.的回答所示, Oracle 12 JDBC标准支持中记录的批量更新不支持检索生成的密钥:

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: 在任何情况下,如果您的驱动程序支持它,那么您的语句prepare应更改为以下代码,以指示驱动程序检索生成的密钥:

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. 注意:您可能需要使用其他生成的密钥之一准备方法( prepareStatement(sql, columnIndexes)prepareStatement(sql, columnNames) ),因为Oracle将使用我的示例中的方法返回ROW_ID

It appears that Oracle 12c does not support combining auto-generated keys with batch update according to the following page: 根据以下页面,Oracle 12c似乎不支持将自动生成的密钥与批量更新相结合:

http://docs.oracle.com/cd/E16655_01/java.121/e17657/jdbcvers.htm 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" 请参阅“检索自动生成的密钥”部分下标有“限制”的小节

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM