Prepared Statement Set Object

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


 

Ads

Tutorials   
Change Column Name in MySQL Java Mysql Connection Example JDBC Example with MySQL Jdbc Mysql Connection String Jdbc Mysql Connection Url Getting Column Names from a database table in Java Count Rows from a Database Table JDBC Example with MySQL Write Text File to Table Inserting Image in Database Table Insert Data in Table Using Stored Procedure Copy One Database Table to Another Copy Table in a MySQL Database Set Timestamp by using the Prepared Statement Set Time by using the Prepared Statement Set Date by using the Prepared Statement Prepared Statement Set Big Decimal Set byte, short and long data types by using the Prepared Statement Set Data Types by using Prepared Statement Using the Prepared Statement Twice Deleting Records using the Prepared Statement Count Records using the Prepared Statement Inserting Records using the Prepared Statement Update Records using Prepared Statement Select Records Using Prepared Statement Prepared Statement With Batch Update Statement Batch Update Prepared Statement Set Object Cross Join Tables in a Specific Database Join tables with the NATURAL RIGHT JOIN operation Join tables with the NATURAL LEFT JOIN operation Join tables in the specific database Delete a Column from a Database Table Delete a Specific Row from a Database Table Deleting All Rows from a Database Table Sum of Column in a Database Table Arrange a Column of Database Table Arrange a Column of Database Table Remove Unique Column in Database Table Make Unique Column in Database Table Change Column Name of a Table Adding a New Column Name in Database Table Retrieving All Rows from a Database Table Inserting values in MySQL database table Retrieving Tables from a Database Deleting a Table from Database Description of Database Table Creating a MySQL Database Table to store Java Types Creating a Database Table Creating a Database in MySQL
Ads

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

Advertisements

Ads
Share on Google+Share on Google+

Prepared Statement Set Object

Posted on: April 13, 2007 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Advertisements

Ads
 

Discuss: Prepared Statement Set Object   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:1
Nirdesh
March 22, 2012
VJTVAEqgSmUh

Unbelievable, how messed up this is. Their are times(atleast twice) i dntfeaiely think i did it super fast(quick refresh/quick code 7second input) not like most of you who always thinks you did it fast enough.Also why does the site fail every time the hour mark comes.

Ads

 

Ads