Saturday, 3 January 2015

What is PreparedStatement interface in JDBC

PreparedStatement interface:

The PreparedStatement interface is a subinterface of Statement. It is used to exeucte parameterized query.

How  PreparedStatement is faster and better than Statement interface ?

The performance of the application will be faster if you use PreparedStatement interface because query is compiled only once. The prepareStatement() method of Connection interface is used to return the object of PreparedStatement.

Syntax:
public PreparedStatement prepareStatement(String query) throws SQLException{  }

Example of PreparedStatement interface that inserts the record:

First create a table in database.

create table employee(id number(8),name varchar2(60),address varchar2(80) );

save as  Insert.java

import java.sql.*;
 class Insert{
 public static void main(String args[]){
 try{
 Class.forName("oracle.jdbc.driver.OracleDriver");
 Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","oracle");
 PreparedStatement stmt=con.prepareStatement("insert into employee values(?,?,?)");
 stmt.setInt(1,111);  //here 1 specifies the first parameter in the query.
 stmt.setInt(2,"Vishwa");
 stmt.setInt(3,"New Delhi");
 int i=stmt.executeUpdate();
 System.out.println(i+" record inserted");
 con.close();
 }catch(Exception e){ System.out.println(e);}
  }
 }

 Ouput: 1 record inserted

 Example of PreparedStatement interface that updates the record:

 save as Update.java

 import java.sql.*;
 class Update{
 public static void main(String args[]){
 try{
 Class.forName("oracle.jdbc.driver.OracleDriver");
 Connection con=DriverManager.getConnection( "jdbc:oracle:thin:@localhost:1521:xe","system","oracle");
 PreparedStatement stmt=con.prepareStatement(" update employee set name=? where id=? ");
 stmt.setString(1,"Alex");  //1 specifies the first parameter in the query i.e. name
 stmt.setInt(2,111);
 int i=stmt.executeUpdate();
 System.out.println(i+" record updated");
 con.close();
 }catch(Exception e){ System.out.println(e);}
  }
 }

 Ouput: 1 record updated

 Example of PreparedStatement interface that retrieve the records of a table:

 save as Retrieve.java

 import java.sql.*;
 class Retrieve{
 public static void main(String args[]){
 try{
 Class.forName("oracle.jdbc.driver.OracleDriver");
 Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","oracle");
 PreparedStatement stmt=con.prepareStatement("select * from employee");
 ResultSet rs=stmt.executeQuery();
 while(rs.next()){
 System.out.println(rs.getInt(1)+" "+rs.getString(2)+" "+r.getString(3);
 }
 con.close();
 } catch(Exception e){ System.out.println(e);}
   }
 }

 Ouput: 111 Alex New Delhi

 Example of PreparedStatement interface that deletes the record:

 save as Delete.java

 import java.sql.*;
 class Delete{
 public static void main(String args[]){
 try{
 Class.forName("oracle.jdbc.driver.OracleDriver");
 Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","oracle");
 PreparedStatement stmt=con.prepareStatement(" delete from employee where id=? ");
 stmt.setInt(1,111);
 int i=stmt.executeUpdate();
 System.out.println(i+" record deleted");
 con.close();
 }catch(Exception e){ System.out.println(e);}
  }
 }

 Ouput: 1 record deleted


No comments:

Post a Comment