Difference between execute() and executeUpdate() for Procedures
Hi,
I am trying to understand the differences between the execute() and the executeUpdate() methods of the java.sql.PreparedStatement.
I am using a CallableStatement to call a Stored Procedure in Oracle. I have tired using the execute() and the executeUpdate() methods and both of them work. Howeer, execute() returns a boolean & executeUpdate() returns an integer.
I am unable to understand the return value of executeUpdate() - if I call a procedure that simply returns a ResultSet in an OUT Parameter, executeUpdate() returns 1. However, the JavaDoc for executeUpdate() states that it :-
[CODE]
Returns:
either (1) the row count for INSERT, UPDATE, or DELETE statements or (2) 0 for SQL statements that return nothing
[/CODE]
I am a little confued with the output.
Can you guys please explain the difference to me ?
Regards,
Sandeep
The boolean returned by execute() indicates if the query returned a ResultSet or not. Of course, insert, update and delete queries wont return ResultSet and select queries will. If ResultSet is returned, its reference can be obtained by getResultSet().
The integer returned by executeUpdate() indicates the number of rows affected, for example,
delete from employee where hours_worked<10
If two employee entries match the criteria and are deleted, 2 is returned by executeUpdate()
The number of affected rows can be obtained using execute() by calling getUpdateCount() if execute() returns false indicating an update query.
The rule of the thumb for clarity is that if you know your statement will return results, use executeQuery(), if you know it wont, use executeUpdate(), if you dont know, use execute() and check for returned boolean for any further action.
Hi,
> The rule of the thumb for clarity is that if you know
> your statement will return results, use
> executeQuery(), if you know it wont, use
> executeUpdate(), if you dont know, use execute() and
> check for returned boolean for any further action.
Yes, it makes perfect sense now ! Thanks a lot - the Duke points are yours ! :)
Regards,
Sandeep