I'm using spring JdbcTemplate
to execute a sql query:
JdbcTemplate template = new JdbcTemplate(ds);
template.execute(sqlInsert); //returns void
How could I get the number of effected rows, as the execute()
method returns void?
* @return the number of rows affected by the execution of the SQL query. * @exception Failed to execute the SQL query. The execute() method executes the given SQL query, which may return multiple results. This method returns a boolean (true/false).
JdbcTemplate will most likely be faster when talking about pure query execution, because a JPA implementation will do more stuff: Parse JPQL (assuming you are using that) creating a SQL query out of that. executing it.
it will return 1 for success and 0 for failure case so, according to it you can process your logic.
executeUpdate() or execute() followed by getUpdateCount() will return the number of rows matched, not updated, according to the JDBC spec.
Call the update method of JdbcTemplate
. It will gives you the number of effected rows as return value.
update
public int update(PreparedStatementCreator psc) throws DataAccessException
Description copied from interface:
JdbcOperations
Issue a single SQL update operation (such as an insert, update or delete statement) using a
PreparedStatementCreator
to provide SQL and any required parameters. APreparedStatementCreator
can either be implemented directly or configured through aPreparedStatementCreatorFactory
.Specified by:
update in interfaceJdbcOperations
Parameters:
psc - object that provides SQL and any necessary parametersReturns:
the number of rows affectedThrows:
DataAccessException
- if there is any problem issuing the updateSee Also:
PreparedStatementCreatorFactory
You can probably use JdbcTemplate.update()
for that case. this will return the number of rows updated or deleted.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With