PreparedStatement Interface In JDBC

  • The PreparedStatement interface is used to execute the parameterized SQL statement.
  • The PreparedStatement interface object represents a precompiled query that can be executed multiple times without compiling it again and again.
  • The java.sql.PreparedStatement interface is a part of JDBC API that describes the PreparedStatement object.
  • The PreparedStatement object is associated with a Connection object.
  • The Connection object submits given SQL statement to the database.
  • The database compiles the given SQL statement.
  • An execution plan is prepared by the database to execute the SQL statement.
  • Database stores the execution plan with a unique id and returns the identity to the Connection object.
  • The Connection object prepares a PreparedStatement object, initializes it with the execution plan identity and returns the reference of PreparedStatement object to the Java application.
  • The setXXX() method the PreparedStatement object are used to set the parameters of the SQL statement.
  • Then the executeXXX() method of the PreparedStatement object is invoked to execute the SQL statement.
  • Finally the result of the SQL statement is sent to the Java application in the form of ResultSet.

Let's see a program using PreparedStatement with parameters:

import java.sql.*;
class PreparedStatementDemo
{
public static void main(String args[])throws Exception
{
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection conn=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE","system","password");
String query="insert into employee values(?,?,?)";
PreparedStatement ps=conn.prepareStatement(query);
ps.setInt(1,103);
ps.setString(2,"Sushh");
ps.setInt(3,40000);
int i=ps.execyteUpdate();
System.out.println(i+"record inserted");
conn.close();
}
}

Output
1 record inserted

Our Services:

Java Training | Python Training | Machine Learning with Python Training | Deep Learning with Python Training | Data Science with Python Training | Projects

SILAN Technology, BBSR
Call to: 0674-2361252

Comment