是否可以在Spring jdbc模板调用中从SQL插入中获取@@ identity?如果是这样,怎么样?
我使用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 != …Run Code Online (Sandbox Code Playgroud) java ×2
jdbc ×2
batch-insert ×1
jdbctemplate ×1
oracle ×1
primary-key ×1
spring ×1
sql-server ×1