Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save prongbang/e3d0ceb7611a57bb70d327d9d186788c to your computer and use it in GitHub Desktop.
Save prongbang/e3d0ceb7611a57bb70d327d9d186788c to your computer and use it in GitHub Desktop.
Insert and get pk (Spring Jdbc Template)

Insert and get pk (Spring Jdbc Template)

```java String sql = "INSERT INTO Employee (name, age) VALUES (?, ?)";

KeyHolder holder = new GeneratedKeyHolder();

jdbcTemplate.update(new PreparedStatementCreator() {

@Override public PreparedStatement createPreparedStatement(Connection conn) throws SQLException {

PreparedStatement ps = conn.prepareStatement(sql, new String[]{ "id" });

ps.setString(1, entity.getName());
ps.setInt(2, entity.getAge());

return ps;

} }, holder);

int id = holder.getKey().intValue();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment