PreparedStatement Interface In Java
PreparedStatement Interface
In JDBC, The PreparedStatement interface is a sub interface of Statement and it is used to execute parameterized query.
The example is given below.
String str="insert into student values(?,?,?,?)";
We are passing the parameter (?) for the values and its value will be set by calling the setter methods of PreparedStatement.
Why we use PreparedStatement?
PreparedStatement is mainly used to improve the performance.
The performance of the Application will be faster, if we use PreparedStatement interface because query is compiled only once.
Get the PreparedStatement object
The PrepareStatement() method of connection interface is used to return the PreparedStatement object.
Syntax
public PreparedStatement prepareStatement(String query)throws SQLException{}
Methods of PreparedStatement interface
public void setInt(int paramIndex, int value)
This method is used to set the integer value to the given parameter index.
public void setString(int paramIndex, String value)
This method is used to set the string value to the given parameter index.
public void setFloat(int paramIndex, float value)
This method is used to set the float value to the given parameter index.
public void setDouble(int paramIndex, double value)
This method is used to set the double value to the given parameter index.
public int executeUpdate()
This method is used to execute the query. It is used to create, drop, insert, update, delete etc.
public ResultSet executeQuery()
This method is used to execute the select query. It returns an instance of ResultSet.
Let’s see an example1 to insert the records, given below.
Code
- import java.sql.*;
- public class StudentDatabase2 {
- public static void main(String args[]) throws Exception {
- Class.forName("org.apache.derby.jdbc.ClientDriver");
- String url = "jdbc:derby://localhost:1527/Student";
- String username = "Student";
- String password = "student";
- Connection conn = DriverManager.getConnection(url, username, password);
- PreparedStatement st = conn.prepareStatement("insert into STUDENT.STUDENTDB values(?,?,?,?)");
- st.setInt(1, 289);
- st.setString(2, "David");
- st.setInt(3, 45);
- st.setString(4, "JTMN college");
- int i = st.executeUpdate();
- System.out.println(i + " :records inserted");
- conn.close();
- }
- }
Output
Let’s see an example2 to update the records, given below.
Code
- import java.sql.*;
- public class StudentDatabase2 {
- public static void main(String args[]) throws Exception {
- Class.forName("org.apache.derby.jdbc.ClientDriver");
- String url = "jdbc:derby://localhost:1527/Student";
- String username = "Student";
- String password = "student";
- Connection conn = DriverManager.getConnection(url, username, password);
- PreparedStatement st = conn.prepareStatement("update STUDENT.STUDENTDB set STUDNAME=? where STUDID=?");
- st.setString(1, "Angel");
- st.setInt(2, 289);
- int i = st.executeUpdate();
- System.out.println(i + " records updated");
- conn.close();
- }
- }
Output
Let’s see an example3, given below, to delete the records.
Code
- import java.sql.*;
- public class StudentDatabase2 {
- public static void main(String args[]) throws Exception {
- Class.forName("org.apache.derby.jdbc.ClientDriver");
- String url = "jdbc:derby://localhost:1527/Student";
- String username = "Student";
- String password = "student";
- Connection conn = DriverManager.getConnection(url, username, password);
- PreparedStatement st = conn.prepareStatement("delete from STUDENT.STUDENTDB where STUDID=?");
- st.setInt(1, 289);
- int i = st.executeUpdate();
- System.out.println(i + " records deleted");
- conn.close();
- }
- }
Output
Let’s see an example4, given below, to retrieve the records.
Code
- import java.sql.*;
- public class StudentDatabase2 {
- public static void main(String args[]) throws Exception {
- Class.forName("org.apache.derby.jdbc.ClientDriver");
- String url = "jdbc:derby://localhost:1527/Student";
- String username = "Student";
- String password = "student";
- Connection conn = DriverManager.getConnection(url, username, password);
- PreparedStatement st = conn.prepareStatement("select * from STUDENT.STUDENTDB");
- ResultSet rs = st.executeQuery();
- while (rs.next()) {
- System.out.println(rs.getInt(1) + " " + rs.getString(2) + " " + rs.getString(3) + " " + rs.getString(4));
- }
- conn.close();
- }
- }
Output
Let’s see an example5, given below, to insert the records until user presses no.
Code
- import java.sql.*;
- import java.io.*;
- public class StudentDatabase2 {
- public static void main(String args[]) throws Exception {
- Class.forName("org.apache.derby.jdbc.ClientDriver");
- String url = "jdbc:derby://localhost:1527/Student";
- String username = "Student";
- String password = "student";
- Connection conn = DriverManager.getConnection(url, username, password);
- PreparedStatement ps = conn.prepareStatement("insert into STUDENT.STUDENTDB values(?,?,?,?)");
- BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
- do {
- System.out.println("Enter STUDID:");
- int id = Integer.parseInt(br.readLine());
- System.out.println("Enter STUDNAME:");
- String name = br.readLine();
- System.out.println("Enter STUDAGE:");
- int age = Integer.parseInt(br.readLine());
- System.out.println("Enter STUDCOLLEGE:");
- String college = br.readLine();
- ps.setInt(1, id);
- ps.setString(2, name);
- ps.setFloat(3, age);
- ps.setString(4, college);
- int i = ps.executeUpdate();
- System.out.println(i + " records affected");
- System.out.println("Do you want to continue: y/n");
- String s = br.readLine();
- if (s.startsWith("n")) {
- break;
- }
- } while (true);
- conn.close();
- }
- }
Output
Summary
Thus, we learnt, JDBC PreparedStatement interface is a sub interface of statement. It is used to execute the parameterized query and also learnt its important methods in Java.