This is how it's done:
KeyHolder keyHolder = new GeneratedKeyHolder();
jdbcTemplate.update(
new PreparedStatementCreator() {
public PreparedStatement createPreparedStatement(Connection connection) throws SQLException {
PreparedStatement ps =
connection.prepareStatement(INSERT_SQL, Statement.RETURN_GENERATED_KEYS);
ps.setString(1, callString);
ps.setString(2, storageInsightDTO.getMetadata().getRc());
return ps;
}
},
keyHolder);
System.out.println("generated key: " + keyHolder.getKey());
Where
1. INSERT_SQL is your native SQL Insert Query
2. Statement
I hope, you know basic Spring, if you come here and looking for the generated key, then above code is going to be very helpful for you.
0 comments :
Post a Comment