Java Design Pattern
Introduction to Java 10
Introduction to Java 11
Introduction to Java 12

JDBC PreparedStatement Interface

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.

JdbcExample2.java

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();
    }

}

Output:
img

Now we will check customer_info table

select * from customer_info

img

//JDBC program to update a record from employee_detail table

JdbcExample3.java

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();
    }

}

Output:
img

Now we will see employee_detail table;

select * from employee_detail

img

//JDBC program to delete a record from employee_detail table

JdbcExample4.java

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();
    }

}

Output:
img

Now we will see employee_detail table;

select * from employee_detail

img


About the Author



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







 PreviousNext