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

Hot Searches
struts-config.xml  web.xml  Java date format  read/write text file using javascript  ArrayList  Insert Data into Database Using Hibernat  create text file using java  insert data to text file using java  Visual Basic Date and Time Display the t  Create table and insert data by sql quer 

  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments

JDBC batch update

                         

JDBC batch update is a collectively called when a group of SQL statements are executed simultaneously to a database as a single unit. The batch is sent to the database in a single request using connection object. The advantage of batch is to reduce the network calls between the front end application and its database rather than executing single SQL statement.

Understand with Example

In this Tutorial we want to understand you an example from JDBC batch update. For this we have a class JDBC Batch Update. Inside the main method we have the list of following step : Before defining class we need to import a java.sql package. This package contains the definition for all the classes which provides network interface to communicate between front end and back end. Loading a Driver  -  The next step is to load a driver, that call a Class.forName( ).This help you in establishing a connection between the front end -backend of application. The DriverManager.get connection( ) - This method return you a connection object, that is used to built connection between url and database.

      con.setAutoCommit(false)  :  This method return you the disable auto commit.

     con.createStatement ( )  : This method return a SQL object. Once your connection is built between your front end and backend, you can retrieve, update the record in the database. A connection object is used to send and execute SQL statement to a backend  database.

      st.addBatches (update ) : This is used to add the values of the parameter in the database. This method return you the modify value in the database using update query in the sql command. The method also shows the number of rows affected in a table.

      executeQuery ( ) : This method is used to retrieve the records from the database using select query.

      rs.next ( ) : This method return you the next successive value in the element series.  

Finally the println print the updated value of the element in the command prompt. Incase there is an exception occurred during a connection between the front end and the backend. The catch block is used, to handle an exception occurred using printstacktrace( ).

        printstacktrace( ): This method prints a stack trace for  Throw able object on the error output stream .

JdbcBatchUpdate.java

import java.sql.*;
public class JdbcBatchUpdate {
    public static void main(String args[]) {
        Connection con = null;
        Statement st = 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);
            con.setAutoCommit(false);// Disables auto-commit.
            st = con.createStatement();
            st.addBatch("update person set cname='Ajay' where id='1'");
            st.addBatch("update person set cname='Komal' where id='2'");
            st.addBatch("update person set cname='Santosh' where id='3'");
            st.executeBatch();
            String sql = "select * from person";
            rs = st.executeQuery(sql);
            System.out.println("No  \tName");
            while (rs.next()) {
                System.out.print(rs.getString(1) + "   \t");
                System.out.println(rs.getString(2));
            }
            rs.close();
            st.close();
            con.close();
        catch (Exception e) {
            e.printStackTrace();
        }
    }

Output

No  	Name
4   	Komal
5   	Ajay
6   	Santosh

Download code

                         

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 
Join and Excel yourself with our Online instructor led training sessions
Integrated Struts 2 Hibernate and JPA Training
Java Training
Hibernate Training
JPA Training
JSF Training
Spring Framework Training
Struts 2 Training
EJB 3 Training
PHP Training
Junit Training
DbUnit Training
Ajax Training
IBM Websphere Training
Design Patterns Training
Weblogic Training
Tomcat Training
JMS Training
Jboss Training
Servlet Training
JSP Training
JavaFX Training
SQL Server Training
TPTP Training
Tell A Friend
Your Friend Name

 

 
 

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 | 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.