PreparedStatement interface in java

PreparedStatement interface

 Provides us to execute stored procedure & dynamic SQL statements
 To create the PreparedStatement object we have to call preparedStatement() method of connection
 The dynamic SQL statement can be executed many times with changing parameters. This provides multiple statement execution.
String sq=“Select * from emp where name=%”;
PreparedStatement p = connection.preparedStatement(sq);

PreparedStatement interface

execute
Executes current sql statement or stored procedure

executeQuery
Executes current sql statement or stored procedure which returns a result set

executeUpdate
Executes sql statement which updates database

setByte
Sets specified parameter to be passed byte variable

setDate
Sets specified parameter to be passed date object

setDouble
Sets specified parameter to be passed double value

setFloat
Sets specified parameter to be passed float value

setInt
Sets specified parameter to be passed Integer value

setLong
Sets specified parameter to be passed Long value

setNULL
Sets specified parameter to a null value

setObject
Sets specified parameter to passed object

setTime
Sets specified parameter to passed time object

clearParameters
It clears all the parameters which are currentlt used by dynamic SQL statement

Example
import java.sql.*;
class test
{
public static void main(String args[])
{
try{
Class.forName(“oracle.jdbc.driver.OracleDriver”);
Connection con=DriverManager.getConnection(“jdbc:oracle:thin:@localhost:1521:xe”,”system”,”system”);
PreparedStatement stmt=con.prepareStatement(“insert into tab values(?,?)”);
stmt.setInt(1,101);
stmt.setString(2,”Adam”);
int i=stmt.executeUpdate();
System.out.println(i+”inserted”);
con.close();
}
catch(Exception e)
{
System.out.println(e);
}
}
}

Leave a Reply

Your email address will not be published. Required fields are marked *