Home Jdbc JDBC batch
Questions:Ask|Latest


 
 

Share on Google+Share on Google+

JDBC batch

Advertisement
JDBC is simply a Java Database Connectivity. This Connectivity provides you an API, that provides you to interact your Java Application in your front end with the backend.

JDBC batch

     

JDBC is simply a Java Database Connectivity. This Connectivity provides you an API, that provides you to interact your Java Application in your front end with the backend. The backend can be SQL-2000,2005,MySql. A batch in JDBC is a set of sql statement  altogether to the database for execution.

Understand with Example

We have already know with the JDBC Connection in previous tutorial. In this Tutorial we want to describe you a code that helps you in understanding a JDBC batch. The code explain you a JDBC,that include collective set of SQL queries executed altogether to the database. For this we define a class JdbcBatch, Inside the main method, we define a url,database,driver name,username and password that helps you to establish a connection an application with the backend database. The list of method helps you in execution of JDBC batch are as given below - 

 Before defining a class, you need to import a java.sql.* :The package import all defined classes, which are required for providing network interface between the application and database.

 Driver loading  : load the driver class by calling a Class.forName ( ) with the driver passed as an argument. Driver Connection provides you to connect  the front end in java application and backend in sql.

con = DriverManager.getConnection(url+db,user,pass)  -The.getConnection ( ) return you an object of connection class.This method attempt to connect a database connection to the specified url.

The create Statement ( ) method return you an object of statement. An statement is used to execute a sql statement in the backend

add Batches( ) :  An object of statement  call a add Batches ( ) method, that set the batch values into specified database.

st.executeBatches ( ) :This method is used to execute the sql Batches.

 execute Query ( ) -  This method return you an object of result set. This object include set of all the values from the table "stu". The next ( ) return you the successive element from a table of the database.Finally the println print the element stored in the database.

JdbcBatch .java

import java.sql.*;
public class JdbcBatch {
    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 stu " +
                    "VALUES('1','name2')");
            st.addBatch("INSERT INTO stu " +
                    "VALUES('2','name3')");
            st.addBatch("INSERT INTO stu " +
                    "VALUES('3','name4')");
            
             st.executeBatch();
            String sql = "select * from stu ";
            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

Advertisements

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: November 17, 2008

Ask Questions?    Discuss: JDBC batch  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
DMCA.com