Monday, 29 April 2013

JDBC - Batch Update Using Statement




JDBC Batch Update Using Statement:-

packagecom.gaurav.jdbcexamples;

importjava.sql.Connection;
importjava.sql.DriverManager;
importjava.sql.ResultSet;
importjava.sql.SQLException;
importjava.sql.Statement;

public class JDBCBatchUpdateUsingStatement{

      /** 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;
            Statement stmt = null;
            ResultSet rs = null;

            try {
                  Class.forName(JDBC_DRIVER);
                  con = DriverManager.getConnection(DATABASE_URL, USERNAME, PASSWORD);
                  String Query = "INSERT  INTO student(STUDENT_ID,FIRSTNAME,LASTNAME,BIRTHDATE,TOTAL_MARKS,DIVISION) VALUES ('1','Kumar','Gaurav','1998-05-12','920','FIRST')";
                  String Query1 = "INSERT  INTO student(STUDENT_ID,FIRSTNAME,LASTNAME,BIRTHDATE,TOTAL_MARKS,DIVISION) VALUES('2','Gaurav','Kumar','1997-11-19','880','FIRST')";
                  String Query2 = "INSERT  INTO student(STUDENT_ID,FIRSTNAME,LASTNAME,BIRTHDATE,TOTAL_MARKS,DIVISION) VALUES('3','Shivam','Kumar','1999-07-11','670','SECOND')";
                  String Query3 = "INSERT  INTO student(STUDENT_ID,FIRSTNAME,LASTNAME,BIRTHDATE,TOTAL_MARKS,DIVISION) VALUES('7','John','Pattinson','1995-03-14','780','FIRST')";
                  String Query4 = "INSERT  INTO student(STUDENT_ID,FIRSTNAME,LASTNAME,BIRTHDATE,TOTAL_MARKS,DIVISION) VALUES('19','Paul','Miller','1999-11-29','590','THIRD')";
                  stmt = con.createStatement();
                  con.setAutoCommit(false);

                  stmt.addBatch(Query);
                  System.out.println("1st Record Inserted");
                  stmt.addBatch(Query1);
                  System.out.println("2nd Record Inserted");
                  stmt.addBatch(Query2);
                  System.out.println("3rd Record Inserted");
                  stmt.addBatch(Query3);
                  System.out.println("4th Record Inserted");
                  stmt.addBatch(Query4);
                  System.out.println("5th Record Inserted");

                  int[] num = stmt.executeBatch();
                  System.out.println("Total number of affected records are-"
                              + num.length);
                  con.commit();
                  String query = "SELECT * FROM student";
                  rs = stmt.executeQuery(query);

                  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 (stmt != null)
                              stmt.close();
                        if (con != null)
                              con.close();
                  } catch (Exception e) {
                        e.getMessage();
                  }
            }
      }
}

Note:- For table structure ref:-




Result:-





Note:- I have deleted the existing records from the table.So, prior to execution of the above program there is no record in the table.

No comments:

Post a Comment