Rehman Rehman - 1 year ago 100
Java Question

Is it a good practice to use JdbcTemplate.update() to execute a stored proc?

Currently I am using

method to execute a stored proc which basically update and inserts some records and it
doesnt return anything.
But I am not sure whether its a good practice to use
method to execute a stored proc. Is there any drawback of using that method?
or do i have to use
method ? I am trying to avoid implementing execute() since it will require lot of code changes in my application.

Code :

jdbcTemplate.update("call test_stored_proc(? , ?)", new Long[] {
userId, statusId });

Answer Source

You may use whichever you wish for this. execute is kind of general API that can run anything and lets you know what kind of result it returns (result set or number of affected rows). update is a special version for queries that either return affected rows or nothing at all.

JdbcTemplate delegates the query ultimately to java.sql.Statement.

From java.sql.Statement Javadoc:

executeUpdate(String sql): Executes the given SQL statement, which may be an INSERT, UPDATE, or DELETE statement or an SQL statement that returns nothing, such as an SQL DDL statement.

execute(String sql): Executes the given SQL statement, which may return multiple results.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download