Prepared Statement is an interface extended from Statement interface.
1) PreparedStatement transfers numbers, String, dates and
large objects between java application and database.
2) PreparedStatement Pre Compiles an Sql statement and that
query can be executed from any number of times by recompilation again. It means
PreparedStatement increases the performance of an Application
3) A Prepared Statement allows to put Parameters (?,?,?)
into sql query to make it as dynamic whenever we want to run same query for
multiple number of times, PreparedStatement Performance is better than
Statement.
Example program for
Prepared Statement :
import
java.util.*; // Util package
import java.io.*; //
IO Pacakage
import java.sql.*; //
it is used for all JDBC Connection.
Class JdbcDemo1{
Public static void
main(String args[]) throws Exception{
Class.forName(“Oracle.jdbc.driver.OracelDriver”);
Connection
con=DriverManager.getConnection(“jdbc:oracle:thin:@localhost:XE”,”scott”,”tiger”);
PreparedStatement
pstmt=con.preparedStatement(“insert into
emp values(?,?,?)”);
// Inserting the data dynamically using “?”.
Scanner s=new Scanner(System.in);
System.out.println(“Enter the Company Id”);
Reading the data dynamically for ?,?,?
}
}
No comments:
Post a Comment