PreparedStatement is an inbuilt interface present in java.sql package.
It is the child interface of Statement interface.
It is used to execute parameterized sql statement.
Let’s see some examples for better clarity.
Here we have used Oracle10g as database.
Now we have a customer_info table like; create table customer_info ( cid varchar2(200) primary key, cname varchar2(200) not null, caddress varchar2(200) not null )
Output: table created
Now we will develop a JDBC program to insert record into customer_info table.
importjava.sql.*; publicclass JdbcExample2 { publicstaticvoidmain(String[] args) throws Exception{ //To Load and Register the Driver Class.forName("oracle.jdbc.driver.OracleDriver"); //Establish the connection Connection con=DriverManager.getConnection("jdbc:oracle:thin: @localhost:1521:xe","system","oracle"); //Create the statement object PreparedStatementps=con.prepareStatement("insert into customer_info values(?,?,?)"); ps.setInt(1, 102); ps.setString(2, "Sourav"); ps.setString(3, "BBSR"); //Execute the sql statement intk=ps.executeUpdate(); System.out.println(k+"row inserted"); //Close the connection con.close(); } }
Now we will check customer_info table
select * from customer_info
//JDBC program to update a record from employee_detail table
importjava.sql.*; publicclass JdbcExample3 { publicstaticvoidmain(String[] args) throws Exception{ //To Load and Register the Driver Class.forName("oracle.jdbc.driver.OracleDriver"); //Establish the connection Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost: 1521:xe","system","oracle"); //Create the statement object PreparedStatementps=con.prepareStatement("update employee_detail set esalary=? where eid=?"); ps.setInt(1, 20000); ps.setInt(2, 103); //Execute sql statement intk=ps.executeUpdate(); System.out.println(k+"row updated"); //Close the connection con.close(); } }
Now we will see employee_detail table;
select * from employee_detail
//JDBC program to delete a record from employee_detail table
packagecom.silan; importjava.sql.*; publicclass JdbcExample4 { publicstaticvoidmain(String[] args) throws Exception{ //To Load and Register the Driver Class.forName("oracle.jdbc.driver.OracleDriver"); //Establish the connection Connection con=DriverManager.getConnection("jdbc:oracle:thin:@ localhost:1521:xe","system","oracle"); //Create the staement object PreparedStatementps=con.prepareStatement("delete from employee_detail where eid=?"); ps.setInt(1, 103); //Execute the sql statement intk=ps.executeUpdate(); System.out.println(k+"row deleted"); //Close the connection con.close(); } }
Now we will see employee_detail table;
select * from employee_detail
Silan Software is one of the India's leading provider of offline & online training for Java, Python, AI (Machine Learning, Deep Learning), Data Science, Software Development & many more emerging Technologies.
We provide Academic Training || Industrial Training || Corporate Training || Internship || Java || Python || AI using Python || Data Science etc