Batch Update Example JDBC

Batch Update JDBC

If 10 records of Employee are to be inserted, writing 10 times insert command is very slow in performance as 10 times the JDBC program should hit the database. To improve the performance, all the 10 insert statements can be converted into a batch and executed at a time. In this case, JDBC hits the database only once (Hibernate framework does the job like this only) and thereby performance increases.

Example on Batch Update JDBC
import java.sql.*;
public class BatchUpdates
  public static void main(String args[]) throws Exception
    Connection con = DriverManager.getConnection("jdbc:odbc:snrao", "scott", "tiger");
    Statement stmt=con.createStatement();

    stmt.addBatch("insert into Employee values(498, 'Reddy', 2000.50)");
    stmt.addBatch("insert into Employee values(499, 'Naidu', 4500.50)");
    stmt.addBatch("delete Employee where empid=101");

    stmt.executeBatch();                   // executes all addBatch() statements (Batch Update JDBC)

    System.out.println("Inserted and Deleted");


executeBatch() executes all the statements included in addBatch() method.

Note 1: If the JDBC basics are not clear, it is advised to go through JDBC Programming Basic Steps where the meaning of Connection, Statement etc. are discussed.

Note 2: To know how to use try-catch-finally with the above code, example program is available at JDBC Example – Create Table.

View All JDBC Examples

Leave a Comment

Your email address will not be published.