Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get Identity from sql batch insert via jdbctemplate.batchupdate

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.

like image 355
pgollangi Avatar asked Nov 10 '22 03:11

pgollangi


1 Answers

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();
like image 167
Yosef Weiner Avatar answered Nov 15 '22 05:11

Yosef Weiner