Statement PreparedStatement and CallableStatement are interfaces of JDBC that are used to interact with the Database, either to execute the insert’s, updates, deletes or to call a stored procedure or to get data.
Lets see the difference between Statement PreparedStatement and CallableStatement.
- Statements are used to execute simple static SQL that are not to be executed multiple times.
- Statements do not accept dynamic parameters to the SQL’s.
- Statements are slow as compared to PreparedStatement and CallableStatement.
- PreparedStatement are used to execute dynamic SQL that are to be executed multiple times and accept dynamic params.
- PreparedStatement are more efficient than Statements as the SQL’s are pre compiled in the Database for PreparedStatement. Efficiency of PreparedStatement is better over Statements when a same query with different param need to be executed.
Since parameters in PreparedStatement are set using Setter methods for a particular Type (VARCHAR,INT etc),
PreparedStatement have a advantage and are used to avoid SQL injections.
- CallableStatement are used to execute Stored procedures.
- CallableStatement also accept dynamic params and also can be useful in avoiding SQL injections.
CallableStatement have 3 types of params – IN, OUT and INOUT.
IN param works same are PreparedStatement params.
OUT params need to be registered before using them.