Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
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 

                         

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

                         

» View all related tutorials
Related Tags: c database query ide table data io column numbers number sum using this id simple tab student university if ie

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
Search Tutorials

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.