Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JdbcTemplate.update() insert return values

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);
like image 435
Sriram Lns Avatar asked Jan 16 '14 23:01

Sriram Lns


2 Answers

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.

like image 83
Turophile Avatar answered Oct 30 '22 03:10

Turophile


jdbctemplate.update will return in integer format as we know. in order to find 0 or 1 just do this below simple code

int i=jdbctemplate.update(.your db call..);

it will return 1 for success and 0 for failure case so, according to it you can process your logic.

like image 34
Rajesh Avatar answered Oct 30 '22 05:10

Rajesh