Monday, 29 April 2013

JDBC - Batch Update Using PreparedStatement




JDBC Batch Update Using PreparedStatement:-


package com.gaurav.jdbcexamples;

import java.sql.Connection;
import java.sql.Date;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class JDBCBatchUpdateUsingPreparedStatement {
     
      /** Configuraing a connection for MySQL databse */

      private static String JDBC_DRIVER = "com.mysql.jdbc.Driver"; // MySQL Driver filename
      private static String USERNAME = "root";
      private static String PASSWORD = "root";
      private static String DATABASE_URL = "jdbc:mysql://localhost:3306/test"; // Database connecting URL for MySQL
     
      public static void main(String args[]){
            Connection con = null;
            ResultSet rs = null;
            PreparedStatement pstmt = null;
            Statement stmt = null;
           
            try{
                        Class.forName(JDBC_DRIVER);
                        con = DriverManager.getConnection(DATABASE_URL,USERNAME,PASSWORD);
                        String query = "INSERT INTO student VALUES(?,?,?,?,?,?)";
                        pstmt = con.prepareStatement(query);
                        con.setAutoCommit(false);
                        pstmt.setInt(1, 12);
                        pstmt.setString(2, "Mouli");
                        pstmt.setString(3, "Sargam");
                        Date date = Date.valueOf("2000-12-23");
                        pstmt.setDate(4, date);
                        pstmt.setInt(5, 854);
                        pstmt.setString(6, "FIRST");
                        pstmt.addBatch();
                       
                        pstmt.setInt(1, 17);
                        pstmt.setString(2, "Soniya");
                        pstmt.setString(3, "Aahuja");
                        date = Date.valueOf("2001-01-19");
                        pstmt.setDate(4, date);
                        pstmt.setInt(5, 951);
                        pstmt.setString(6, "FIRST");
                        pstmt.addBatch();
                       
                        pstmt.setInt(1, 33);
                        pstmt.setString(2, "Alizabeth");
                        pstmt.setString(3, "Bond");
                        date = Date.valueOf("2002-03-05");
                        pstmt.setDate(4, date);
                        pstmt.setInt(5, 575);
                        pstmt.setString(6, "THIRD");
                        pstmt.addBatch();
                       
                        int count[] = pstmt.executeBatch();
                              System.out.println("Number of affected records in the db are-"+count.length);
                              con.commit();
                              String queryAfterInsert = "SELECT * FROM student";
                              stmt = con.createStatement();
                              rs = stmt.executeQuery(queryAfterInsert);
                             
                              System.out.println("STUDENT_ID" + "\t" + "FIRSTNAME" + "\t"
                                          + "LASTNAME" + "\t" + "BIRTHDATE" + "\t" + "TOTAL_MARKS"
                                          + "\t" + "DIVISION");
                              while (rs.next()) {
                                    System.out.println("\t" + rs.getInt(1) + "\t" + rs.getString(2)
                                                + "\t\t" + rs.getString(3) + "\t\t" + rs.getDate(4)
                                                + "\t" + rs.getInt(5) + "\t\t" + rs.getString(6));
                              }
                                         
            } catch (SQLException sqe) {
                  sqe.printStackTrace();
            } catch (Exception e) {
                  e.printStackTrace();
            } finally {
                  try {
                        if (rs != null)
                              rs.close();
                        if (pstmt != null)
                              pstmt.close();
                        if (con != null)
                              con.close();
                  } catch (Exception e) {
                        e.getMessage();
                  }
            }

           
      }
}

For Table data structure ref:-http://www.javatechtipssharedbygaurav.com/2013/04/jdbc-statement.html

Result:-





Note:- Before the execution of above program there was top five records in the student table and the last three records was inserted as the successful execution of the above program.

No comments:

Post a Comment