Statement, PreparedStatement & CallableStatement

Posted By on April 29, 2016

Download PDF
CallableStatement Interface
Transaction Management in JDBC
  • Once a connection is obtained we can interact with the The JDBC Statement, CallableStatement, and PreparedStatement interfaces define the methods and properties that enable you to send SQL or PL/SQL commands and receive data from your database.
  • They also define methods that help bridge data type differences between Java and SQL data types used in a database
  • Following table provides a summary of each interface’s
Interfaces Recommended Use
Statement Use for general-purpose access to your database. Useful when you are using static SQL statements at runtime. The Statement interface cannot accept parameters.


Use when you plan to use the SQL statements many times. The PreparedStatement interface accepts input parameters at runtime.


Use when you want to access database stored procedures. The CallableStatement interface can also accept runtime input parameters.


  • The Statement Interface:
    • The Statement  interface defines  a  standard  abstraction to execute  the  SQL statements requested by a user and return the results by using the ResultSet object.
    • The Statement interface is created after the connection to the specified database is
    • The object is created using the createStatement() method of the Connection interface, as shown in following code snippet:

Connection con = DriverManager.getConnection(url, “username”, “password”);

Statement stmt = con.createStatement();


  • The PreparedStatement Interface:
    • The PreparedStatement interface is subclass of the Statement interface, can be used to represent a precompiled query, which can be executed multiple
    • The object is created using the prepareStatement() method of Connection interface, as shown in following snippet:

Connection con = DriverManager.getConnection(url, “username”, “password”); String query = “insert into mytable values(? ,? ,?)”;

PreparedStatement ps = con.prepareStatement(query);

ps.setString(1,”Arjun”); ps.setInt(2,100); ps.setDouble(3,111.32);  int n = ps.executeUpdate();


  • Advantages of PreparedStatement:
    • Improves the performance of an application as compared to the Statement object that executes the same query multiple
    • Inserts or  updates  the  SQL  99  data  types  columns,  such  as  BLOB,  CLOB,  or OBJECT, with the help of setXXX() methods.
    • Provides the programmatic approach to set the
  • Disadvantages of PreparedStatement:
    • The main disadvantage of PreparedStatement is that it can represent only one SQL statement at a


  • The CallableStatement Interface:
  • We need to use setLookAndFeel() method of the UIManager to set the Look &
  • As an argument to above method we can pass either of the following
    • swing.metal.MetalLookAndFeel
  • The content of the content pane are updated using the updateComponentTreeUI()
CallableStatement Interface
Transaction Management in JDBC

Download PDF