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 insert

                         

JDBC Batch Insert is a set of SQL Statements sent to the database and executed as a single unit. The Statement object performs the batch update.

Understand with Example

The Tutorial depicts you an example from JDBC Batch Insert, An empty array object is linked, whenever a statement object is created. The multiple SQL statements is added to the empty array. This  statements is executed as a batch. The con.setAutoCommit( ) is used to disable the auto commit.The batch contain three SQL INSERT statement added to a batch using the addBatch ( ).The code show you the list of method required to carry out JDBC Batch Insert are as follows - 

    Driver loading -  Once the sql package is imported, the second step is the loading of a driver class that call a forclass.name with driver name passed as argument. Once your driver is loaded you connect your front end java application to the backend database. If there is an exception in try block, the catch block subsequently handle the exception.

  DriverManager.getconnection ( ) : This is used to establish a connection  between  database and respective url.

. con.createStatement ( ) : This is used  to provide you a Sql object. On establishing a connection ,your application can interact with backend  database. An connection object is used to send and execute Sql statement to a backend database.

   st.addbatch( ) : This method is used to add the sql parameter to the batch of command.

   executeQuery ( ) : This method is used to return the value obtained from select command and is stored in result set object.rs.next( ) - This method is used to retrieve the element from a database in successively sequence.

    The print ln print the element from rs.getString ( ),which return you the value of elements from  the database.

JdbcBatchInsert.java

import java.sql.*;
public class JdbcBatchInsert {
    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("INSERT INTO person VALUES('4','Komal')");
            st.addBatch("INSERT INTO person VALUES('5','Ajay')");
            st.addBatch("INSERT INTO person VALUES('6','Santosh')");
            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 
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.