JDBC Statements

There are 3 types of JDBC statements:


1. Statement
2. PrepparedStatement
3. CallableStatement

Statement


  • Statement is an interface which is in java.sql.package 
  • Statement object can be created using one of the following methods of connection interface  
                  a) Statement createStatement();
                  b) Statement createStatement(int,int)
                  c) Statement creatStatement(int,int,int)
  • Once Statement object is created, you can call one of the following methods to submit the SQL statement to database
                   a) int executeUpadte(String sql)
                   b) boolen execute(String sql)
                   c) ResultSet executeQuery(String sql)
         
              executeUpdate(): method can be used to submit insert,update and delete SQL statement. It return only integer which is no of records inserted or updated or deleted.


  execute(): method can be use to submit insert,update and delete SQL statement. It returns boolen which represent wheather records are inserted, updated or deleted or not


executeQuery() : method can be use to submit select SQL statement. It returns ResultSet object which contain records returned by the select SQL statement.


  • Using one Statement object you can submit any type of sql statement to database.
  •  When you submit the SQL statement to database using statement object then SQL statement will be compiled and executed every time.
PreparedStatement


  • PreparedStatement is an interface which is extending statement and in java.sql.package.
  • It can be created using one of following methods of connection interface
                    a) PreparedStatement PreparedStatement();                  b) PreparedStatement PreparedStatement(int,int)
                  c) PreparedStatement PreparedStatement(int,int,int)
  • Once PreparedStatement object is created you can call one of the following methods to submit the SQL statement to database:
                        a) int executeUpadte()
                   b) boolen execute()
                   c) ResultSet executeQuery()
  •    Using one PreparedStatement object you can submit only one type of SQL Statement to database
  •  When you submit the SQL statement to database using PreparedStatement object then SQL statement will be compiled only once at first time and will be executed every time directly


CallableStatement


  • CallableStatement is an interface which is extending PreparedStatement and is in java.sql.package
  • CallableStatement is designed to invoke the stored procedure running in the DB
  • CallableStatement object can be created using one of the following methods of connection interface:
                     a) CallabeStatement prepareCall(String);                  b) CallabeStatement prepareCall(String,int,int)
                  c) CallabeStatement prepareCall(String,int,int,int)
  • Once CallableStatement is created you can call one of the following methods to submit the procedure call to database
                         a) int executeUpadte()
                   b) boolen execute()
                   c) ResultSet executeQuery()


  • Stored procedure is a pre compiled procedure which will be executed directly so you ca save top of resultset-response time.