1

When inserting using jdbctemplate, I am doing this:

getJdbcTemplate().update("insert users (...) values(?,?,?)", user.get...);

  1. How do I get the inserted id back from msql? (it is the primary key)

For updates, is it possible to return a boolean if the update was successful?

getJdbcTemplate().update("delete users where id = ?", id); 

1 Answer 1

7

JdbcTemplate.update() returns number of rows affected - so you not only know that delete/update was succesfull, you also now how many rows were deleted/updated.

To get generated (from sequence) primary keys, use org.springframework.jdbc.core.JdbcTemplate.update(PreparedStatementCreator, KeyHolder) method which allows you to pass e.g. org.springframework.jdbc.support.GeneratedKeyHolder which will collect your keys.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.