Java – how to insert batch generated keys from JDBC into oracle?
•
Java
I use JDBC to batch insert many records
I'm using 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'm considering using ps.executeupdate() and ps.getgeneratedkeys() in the loop to get the desired results Any other solution?
Solution
It seems that Oracle 12C does not support the combination of automatically generated keys and batch updates according to the following pages:
http://docs.oracle.com/cd/E16655_01/java.121/e17657/jdbcvers.htm
See the section labeled "restrictions" in the section "retrieving automatically generated keys"
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码