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

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
JDBC Prepared Statement Update 
 

The Tutorial illustrates a code that help you in understanding JDBC Prepared Statement Update.

 

JDBC Prepared Statement Update

                         

The Update  Statement in SQL updates the existing records in a table. In JDBC the Prepared Statement Update is used to update the SQL statement, using where clause, that specify which records is updated.

Understand with Example

The Tutorial illustrates a code that help you in understanding JDBC Prepared Statement Update. The code include a class Jdbc Prepared Statement, this class include a main method ( ), Inside the main method we have a list of steps -

 Importing a package java.sql - This package provides you a network interface, that enables you to communicate between front end application -back end. Loading a driver by calling a class.forName ( ),that accept a driver class as argument.

  DriverManager.get Connection ( ) -The Driver Manager call a getConnection ( ) method, return you a connection object, built a connection between url and database.

 The connection object call a prepareStatement ( ),that is used to update the table stu using where clause, specify the name of record to be updated at runtime. The set String XXX return you a set value in the place of question mark holder. The executeQuery ( ) return you the record set from a updated table in the database.

  Finally the println print the updated table from the database in output. The catch block caught the exception in try block.

JdbcPreparedstatementUpdate.java

import java.sql.*;

public class JdbcPreparedstatementUpdate {

    public static void main(String args[]) {

        Connection con = null;
        PreparedStatement pst = null;
        ResultSet rs = null;

        String url = "jdbc:mysql://localhost:3306/";
        String db = "komal";
        String driver = "com.mysql.jdbc.Driver";
        String user = "root";
        String pass = "root";

        try {
            Class.forName(driver);
            con = DriverManager.getConnection(url + db, user, pass);
            
            pst = con.prepareStatement("update stu set class=? where id =?");
            
            pst.setString(1"11");
            pst.setString(2"1");
            
            pst.executeUpdate();
            
            
            pst = con.prepareStatement("select * from stu");
            rs = pst.executeQuery();

            System.out.println("Id\tName\tClass");
            while (rs.next()) {
                System.out.print(rs.getString(1) + "\t");
                System.out.print(rs.getString(2) + "\t ");
                System.out.println(rs.getString(3));
            }

            rs.close();
            pst.close();
            con.close();
            
        catch (Exception e) {
            System.out.println(e);
        }
    }
}

Output

Id	Name	Class
1	komal	 11
2	santosh	 11
3	rakesh	 9
4	ajay	 11
5	bhanu	 10

Download code

                         

» View all related tutorials
Related Tags: sql mysql c ant jdbc help tutorial ria this for to drive e des in sta m nt wan j

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.