I want to get inserted/updated row IDs(PrimaryKey) from org.springframework.jdbc.core.JdbcTemplate.batchUpdate
Is there any way to use KeyHolder like this to get inserted/update row IDs.
There is not, probably because the JDBC specification does not require getGeneratedKeys
to work with executeBatch()
, as noted here. If your driver does support it, you'll need to use plain old JDBC to access the resultset. The code would be something like this:
PreparedStatement ps = conn.prepareStatement("insert into ... values (?)", Statement.RETURN_GENERATED_KEYS);
ps.setXXX(1, value1);
ps.addBatch();
ps.setXXX(1, value2);
ps.addBatch();
ps.executeUpdate();
ResultSet rs = ps.getGeneratedKeys();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With