java – 如何从JDBC中批量生成的密钥插入到Oracle中?
发布时间:2020-12-14 16:43:36 所属栏目:Java 来源:网络整理
导读:我使用JDBC批量插入插入许多记录. 有没有办法获得每个记录生成的密钥? 可以使用ps.getGeneratedKeys()和批量插入吗? 我正在使用oracle.jdbc.OracleDriver final String insert = "Insert into Student(RollNumber,Name,Age) values(StudentSEQ.nextval,?,?
|
我使用JDBC批量插入插入许多记录.
有没有办法获得每个记录生成的密钥? 可以使用ps.getGeneratedKeys()和批量插入吗? 我正在使用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);
}
我正在考虑在循环中使用ps.executeUpdate()以及ps.getGeneratedKeys()来获得所需的结果.任何其他解决方案? 解决方法
看来Oracle 12c不支持按照以下页面将自动生成的密钥与批量更新相结合:
http://docs.oracle.com/cd/E16655_01/java.121/e17657/jdbcvers.htm 请参阅“检索自动生成的密钥”一节中标有“限制”的小节. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
