Showing posts with label Statement.. Show all posts
Showing posts with label Statement.. Show all posts

Wednesday, 29 August 2012

Difference between Statement , Prepared Statement and Callable statement .


Statement:  

Statement is used to execute normal SQLs , it won't take any parameters. Generally preferable for DDL statements and normal SQLs without any parameters. 

Statement st=conn.createStatement();
st.execute("create table tableA (columnA int not null, columnB varchar(255) )");

Prepared Statement:

Prepared statement is used to execute dynamic SQLs with parameters, it extends Statement interface.
If you are executing the same query multiple times, its preferable to use to the prepared statement , since it pre-compiles the statements

PreparedStatement prepStmt= conn.prepareStatement("update tableA set column1=? where column2=?");

prepStmt.setString(1, "test");  
prepStmt.setInt(2, 444);


Callable Statement: Execute a call to a database stored procedure. it extends statement interface . It allows the user to send parameters to procedure and retrieve the data from procedure.


CallableStatement cStmt= conn.prepareCall("call procA(?,?,?)")
cStmt.setString(1,"test");
cStmt.setInt(2,444);
cStmt.registerOutParameter(3);
cStmt.execute();