Home Jdbc Jdbc-mysql Prepared Statement Set Object
Questions:Ask|Latest

 
 

Share on Google+Share on Google+

Prepared Statement Set Object

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

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

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: April 13, 2007

Related Tutorials

Ask Questions?    Discuss: Prepared Statement Set Object   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
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.
DMCA.com