JdbcTemplate.update() insert return values

Sriram Lns picture Sriram Lns · Jan 17, 2014 · Viewed 39.1k times · Source

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

What will be the return value if I try to insert a row.

Is it possible to get return value as "0"??

 private static final String insertSql =
 "INSERT INTO employee (" +
 " name, " +
 " surname, " +
 " title, " +
 " created) " +
 "VALUES (John, Smith, Software developer, new Date())";
 int row = template.update(insertSql, params, types);

Answer

Turophile picture Turophile · Jan 17, 2014

Yes, in theory you should get 0 or 1, but if no row was inserted, it would be due to an error, so a DataAccessException would be thrown, which is how you would know that something went wrong and no row was created.