Contact Us
   
   
 
   
   
 
     
 
 
JSP Tutorial
JDBC Tutorial
 
Book Must Read
 
 

Create Statement or PreparedStatement

 
 
Print   Email
 
     
 

Statement

Statement is object which send SQL query to database. Statement sends each and every time SQL statement to database server for execution. These Statements can be insert, update, delete, create table, select or any. Statement object can be created by connection objects with createStatement(). Statement is in java.sql.Statement

<%
  //Connection conn = null; 
  //conn is Connection object used in createStatement
         
  Statement stm=null;
       
  stm=conn.createStatement();
  stm.executeUpdate("insert into tableName values('FieldName1','FieldName2')");
%>

PreparedStatement

Statement is simple SQL statement and takes no parameters, execute and compile every time when request is generated to database server. PreparedStatement is precompiled SQL statement and reside in PreparedStatement object. This PreparedStatement object executes multiple times SQL statement without compiling it again and again. This is kind of caching SQL statement and execute on parameters specification. First time when it executes, it runs slow but after that it runs much faster than simple Statement Object. Sometimes it is called dynamic statement because it takes parameter setter option.
A SQL statement is given inside when PreparedStatement is creating. PreparedStatement is in java.sql.PreparedStatement

<%
 //Connection conn = null;
 
 PreparedStatement pstm=null;
 
 pstm=conn.prepareStatement("select * from TableName where iEmpID=?");

%>

This PreparedStatement has one parameter and need to set this parameter.

<%
  //Connection conn = null; 
  //conn is Connection object used in prepareStatement
 
 ResultSet rs=null;
 PreparedStatement pstm=null;

 String VariableName="value";

 pstm=conn.prepareStatement("select * from TableName where iEmpID=?");
 pstm.setString(1,VariableName);
 
 rs=pstm.executeQuery();

%>

CallableStatement

CallableStatement is used to execute Stored Procedure on database server. Stored Procedure is set of SQL statement which is stored at database server. Work on these SP we need CallableStatement object. Stored procedure can be executed with call statement on database, same thing use here also. CallableStatement also can take parameters.

<%
  //Connection conn = null; 
  //conn is Connection object used in preparedCall

  CallableStatement cstm=null;
    
  cstm=  conn.prepareCall("call StoredProcedure(?,?)");

  cstm.setString(1, variable1);
  cstm.setInt(2, variable2);
%>

 

 

 

     
 
If You Like this Article, share this

 
 
     
   
     
     
   
     
     
     
 
  Privacy Policy