Prepared Statement Set Object

In JDBC tutorial we are going to learn about the PreparedStatement and how to use with setObject method.

Prepared Statement Set Object

Prepared Statement Set Object 

     

In JDBC tutorial we are going to learn about the PreparedStatement and how to use with setObject method. 

PreparedStatement: This is an interface of java.sql package which extends Statement interface. If you want to execute Statement object  many times then we should use PreparedStatement object as it reduces the execution time. PreparedStatement object is faster than the Statement object as it is precompiled.  In PreparedStatement we use IN parameter whose values are not known when the Sql statement is created. So we use "?" as a IN parameter and we also know it by the name of parameter marker. In this interface the modification has been done on the methods execute, executeQuery and executeUpdate. These methods are modified in such a way so that they take no argument.

Description of program:

In this program we are going to insert data in the database by using the setObject method of PreparedStatement interface. Before going into the details of the program we should firstly need to establish the connection with MySQL database by the help of JDBC driver. After establishing the connection now we will  insert the data in setObject method. If the data gets added in the database table then it will display a message "Record is added in the table." otherwise it will show "SQL statement is not executed!".

Description of code:

prepareStatement(String sql):
This method returns the PreparedStatement object for sending the parameterized SQL statement to the database that contains the pre-compiled SQL statement. Here the pre- compiled means once the statement has been compiled then it will not compile the same thing again. It takes the string type arguments which contains one or more '?' parameter placeholders. 

setObject(int par_index, object obj):
It is used for setting the values of parameterized index by using the given object in this method. It takes two arguments to given below:  
  par_index: It specifies the parameter like: the first is 1, second is 2, ......
  object obj: It contains the parameter values to given by the users.

Here is the code of program:

import java.sql.*;

public class PreparedStatementSetObject{
  public static void main(String[] args) {
  System.out.println("Prepared Statement Set Array Example!");
  Connection con = null;
  try{
  Class.forName("com.mysql.jdbc.Driver");
  con = DriverManager.getConnection
("jdbc:mysql://localhost:3306/jdbctutorial","root","root");
  try{
  PreparedStatement prest = con.prepareStatement("insert emp_sal values(?,?)");
  prest.setObject(1,"Sushil");
  prest.setObject(2,15000);
  int n = prest.executeUpdate();
  System.out.println(n + " Record is added in the table.");
  con.close();
  }
  catch (SQLException s){
  System.out.println("SQL statement is not executed!");
  }
  }
  catch (Exception e){
  e.printStackTrace();
  }
  }
} 

Download this example.

  Database Table: emp_sal

Emp_name Emp_sal
ghtdfgl 5455
Raja Ram 545

Output of program:

C:\vinod\jdbc\jdbc\PreparedStatement>javac PreparedStatementSetObject.java

C:\vinod\jdbc\jdbc\PreparedStatement>java PreparedStatementSetObject
Prepared Statement Set Array Example!
1 Record is added in the table.

After executing the program:

Database Table: emp_sal

Emp_name Emp_sal
ghtdfgl 5455
Raja Ram 545
Sushil 15000

Tutorials

  1. Mapping MySQL Data Types in Java
  2. Connecting to a MySQL Database in Java
  3. Creating a Database in MySQL
  4. Creating a Database Table
  5. Creating a MySQL Database Table to store Java Types
  6. Description of Database Table
  7. Deleting a Table from Database
  8. Retrieving Tables from a Database
  9. Inserting values in MySQL database table
  10. Retrieving All Rows from a Database Table
  11. Adding a New Column Name in Database Table
  12. Change Column Name of a Table
  13. Make Unique Column in Database Table
  14. Remove Unique Column in Database Table
  15. Arrange a Column of Database Table
  16. Arrange a Column of Database Table
  17. Sum of Column in a Database Table
  18. Deleting All Rows from a Database Table
  19. Delete a Specific Row from a Database Table
  20. Delete a Column from a Database Table
  21. Join tables in the specific database
  22. Join tables with the NATURAL LEFT JOIN operation
  23. Join tables with the NATURAL RIGHT JOIN operation
  24. Cross Join Tables in a Specific Database
  25. Prepared Statement Set Object
  26. Statement Batch Update
  27. Prepared Statement With Batch Update
  28. Select Records Using Prepared Statement
  29. Update Records using Prepared Statement
  30. Inserting Records using the Prepared Statement
  31. Count Records using the Prepared Statement
  32. Deleting Records using the Prepared Statement
  33. Using the Prepared Statement Twice
  34. Set Data Types by using Prepared Statement
  35. Set byte, short and long data types by using the Prepared Statement
  36. Prepared Statement Set Big Decimal
  37. Set Date by using the Prepared Statement
  38. Set Time by using the Prepared Statement
  39. Set Timestamp by using the Prepared Statement
  40. Copy Table in a MySQL Database