17 December 2014

java code of batch operation


package com.nhc.personal;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.Statement;
 /**  
  *  
  * @author Niraj Chauhan  
  */
public class CommitWriteBatchTestFlight {
 
 public static void main(String[] dataTrays){
  System.out.println("--BEGIN--");
  String commitCommand = "COMMIT WORK WRITE BATCH";
  
  final String TBL_NAME = "PLAYERS";
  final String COL_ID = "ID";
  final String COL_NAME = "NAME";
  final String COL_TEAM = "TEAM";
  final String COL_TOTAL_RUNS = "TOTAL_RUNS";
  
  String createTableQuery =  "CREATE TABLE PLAYERS ( " +
          "ID INT, " +
          "NAME VARCHAR(255), " +
          "TEAM VARCHAR(255), " +
          "TOTAL_RUNS VARCHAR(255)" +
         ")";
  
  try {
    Class.forName("oracle.jdbc.driver.OracleDriver");
    Connection connection = DriverManager.getConnection("jdbc:oracle:thin:@192.168.8.87:1521:aaadb","netvertextrunk","netvertextrunk");
    
    Statement stmt = connection.createStatement();
    stmt.execute(createTableQuery);
    System.out.println("Table create Successfully");
    stmt.close();
    
    PreparedStatement commitPrepStmt = connection.prepareStatement(commitCommand);
    PreparedStatement prepStmt = connection.prepareStatement("INSERT INTO PLAYERS (ID, NAME, TEAM, TOTAL_RUNS) VALUES (?, ?, ?, ?)");
    
    long startTime = System.currentTimeMillis();
    
    for(int i=1; i<=11; i++){
     prepStmt.setInt(1, i);
     prepStmt.setString(2, "Player"+i);
     prepStmt.setString(3, "Team"+i);
     prepStmt.setString(4, "100"+i);
     prepStmt.executeUpdate();
    }
    prepStmt.close();
    
    //connection.commit();
    //System.out.println("Execution Time: "+(System.currentTimeMillis()-startTime)+" ms");
        
    commitPrepStmt.executeUpdate();
    System.out.println("Execution Time: "+(System.currentTimeMillis()-startTime)+" ms");
    
    connection.close();
    
  } catch (Exception e) {
   System.out.println("Error while executing preparedstatement, Reason: "+e.getMessage());
  }
  
  System.out.println("--END--");
 }
}