Bulk insert in java

In some scenarios we will need to perform bulk inserts in database using java.
Below is the well tested code which can be used to retrieve data from a table using select query and insert into another table using java addBatch method.

In the below code we have used Autocommit  set to false so that if any error in the complete transaction, the data will be rolled back. This is done using successflag in the below code.


FIle: BulkUpdate.java

package REPORTS;

import java.sql.*;
import java.text.*;
import java.util.*;

import connections.*;

public class BulkUpdate {


Connection con = null;
ResultSet rs=null;
        //Set number of records to be inserted per batch
        batchSize =500;

public void bulkInsertDB() {

try{

successflag=0;
count = 0;

con = new ConnectionDB().returnConnection();
System.out.println("creating ORACLE DB connection FOR DATA RETRIEVAL");


//INSERT TO DB CONNECTIONS
con = new ConnectionDB().returnConnection();
System.out.println("creating ORACLE DB connection FOR INSERTION");
con.setAutoCommit(false);


String sql="select * from VW_GET_DATA";
Statement stmt=con.createStatement();
                        rs stmt.executeQuery(sql);
           
 //BULK INSERT          
            String sql_insert = "insert into TABLE (ID,NAME,LEVEL) " +
            "values " +
            "(?,?,?)";

            PreparedStatement ps = con.prepareStatement(sql_insert);
           
         
            while(rs.next())
{
 
            ps.setString(1, rs.getString("ID"));
                ps.setString(2, rs.getString("NAME"));
                ps.setString(3, rs.getString("LEVEL"));

           
            ps.addBatch();
           
                if(++count % batchSize == 0) {
               
               
                  ps.executeBatch();
               
                }//if  
             
  }//WHILE
           
            //INSERTING REMAINING RECORDS
            ps.executeBatch();
           
           
            stmt.close();
            ps.clearBatch();
            ps.close();
           
                     
 //BULK INSERT END        
 
          }
catch(Exception e1) {

successflag=1;
System.out.println("ERROR:"+e1);
//SEND MAIL IF NEEDED ON FAILURE IN BULK INSERT
//YOUR CODE

}
finally
     {
     if(con != null)
     {
     try
     {
   
     if(successflag == 0)
     {
    con.commit();
     }
     else
     {
     con.rollback();
     }
   
     if (con != null) {
    con.close();
   
   
    }
   
   
     }
     catch(Exception e)
     {
     e.printStackTrace();
     }
     }
     }
 
}



public static void main(String[] args) {
BulkUpdate obj = new BulkUpdate();
obj.bulkInsertDB();


}


}

No comments:

Post a Comment