Statement vs. PreparedStatement vs. CallableStatement

❮ Previous Next ❯

Statement vs. PreparedStatement vs. CallableStatement

Statement

Statement interface is used to execute normal SQL queries. You can not pass the parameters to SQL query at run time using this interface. When we are executing a particular SQL query only once, then we need to use this interface. The performance of this interface is also very less .In most of time, Statement interface is used for DDL statements like CREATE, ALTER, DROP etc.Example

//Creating The Statement Object
Statement st = con.createStatement();
//Executing The Statement
st.executeQuery("select * from employee");

PreparedStatement

PreparedStatement is used to execute dynamic or parameterized SQL queries. PreparedStatement is the child interface of Statement interface. It is recommended to use PreparedStatement when we are executing a particular SQL query multiple times. It gives better performance than Statement interface. Because, PreparedStatement are precompiled and the query plan is created only once irrespective of how many times you are executing that query.

//Creating PreparedStatement object
PreparedStatement ps = con.prepareStatement("update STUDENT set NAME = ? where ID = ?");
//Setting values to place holders using setter methods of PreparedStatement object
ps.setString(1, "Akshay");
ps.setInt(2, 101);
//Executing PreparedStatement
pstmt.executeUpdate();

CallableStatement

CallableStatement is used to call stored procedures or functions. CallableStatement extends PreparedStatement. The performance of this interface is higher than the other two interfaces. Because, it calls the stored procedures which are already compiled and stored in the database server.

//Creating CallableStatement object
CallableStatement cs = con.prepareCall("{call ProcedureName(?, ?, ?)}");
//Use cs.setter() methods to pass IN parameters
//Use cs.registerOutParameter() method to register OUT parameters
//Executing the CallableStatement
cs.execute();
//Use cs.getter() methods to retrieve the result returned by the stored procedure
Statement PreparedStatement CallableStatement
It is used to execute normal SQL queries. It is used to execute parameterized or dynamic SQL queries. It is used to call the stored procedures or functions.
It is preferred when a particular SQL query is to be executed only once. It is preferred when a particular query is to be executed multiple times. It is preferred when the stored procedures are to be executed.
You cannot pass the parameters to SQL query using this interface. You can pass the parameters to SQL query at run time using this interface. You can pass 3 types of parameters using this interface. They are – IN, OUT and IN OUT.
This interface is mainly used for DDL statements like CREATE, ALTER, DROP etc. It is used for any kind of SQL queries which are to be executed multiple times. It is used to execute stored procedures and functions.
The performance of this interface is very low. The performance of this interface is better than the Statement interface . The performance of this interface is high.
❮ Previous Next ❯

Follow Us




  Tutorials

Popular Links

Contact Us

address Plot No-N6/530,2ND Floor
Nayapalli,Jayadev Vihar
      Bhubaneswar-751015
      Email: info@silantechnology.com
      Phone: 0674-2361252
address
39877, sundale dr, apt#101, Fremont, California 94538, USA
       Phone: +1(262)388-7619

facebook twitter google linked in

© 2018 Silan Technology. All Rights Reserved