Get id from sql package insert via jdbctemplate.batchupdate

I want to get inserted / updated row identifiers (PrimaryKey) from org.springframework.jdbc.core.JdbcTemplate.batchUpdate

Is it possible to use KeyHolder, for example, to insert / update row identifiers.

+5
source share
1 answer

No, probably because the JDBC specification does not require getGeneratedKeys to work with executeBatch() , as indicated here . If your driver supports it, you need to use plain old JDBC to access the result set. 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(); 
+1
source

Source: https://habr.com/ru/post/1200292/


All Articles