8

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.

Community
  • 1
  • 1
pgollangi
  • 848
  • 3
  • 13
  • 28

1 Answers1

1

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();
Community
  • 1
  • 1
Yosef Weiner
  • 5,432
  • 1
  • 24
  • 37