Difference between PreparedStatement and CallableStatement
1. CallableStatement :
It is used when you want to use the database stored procedures. CallableStatement can accept runtime input parameters.
Example –
//Creating CallableStatement object CallableStatement GFG = con.prepareCall("{call anyProcedure(?, ?, ?)}"); //Use GFG.setter() methods to pass IN parameters //Use GFG.registerOutParameter() method to register OUT parameters //Executing the CallableStatement GFG.execute(); //Use GFG.getter() methods to retrieve the result
2. PreparedStatement :
It is used when you want to use SQL statements many times. The PreparedStatement interface accepts input parameters at runtime.
Example –
//Creating the PreparedStatement object PreparedStatement GFG = con.prepareStatement("update STUDENT set NAME = ? where ID = ?"); //Setting values to place holders GFG.setString(1, "RAM"); //Assigns "RAM" to first place holder GFG.setInt(2, 512); //Assigns "512" to second place holder //Executing PreparedStatement GFG.executeUpdate();
Difference between CallableStatement and PreparedStatement :
CallableStatement | PreparedStatement |
---|---|
It is used when the stored procedures are to be executed. | It is used when SQL query is to be executed multiple times. |
You can pass 3 types of parameter IN, OUT, INOUT. | You can pass any type of parameters at runtime. |
Used to execute functions. | Used for the queries which are to be executed multiple times. |
Performance is very high. | Performance is better than Statement. |
Used to call the stored procedures. | Used to execute dynamic SQL queries. |
It extends PreparedStatement interface. | It extends Statement Interface. |
No protocol is used for communication. | Protocol is used for communication. |
Contact Us