How do I create a batch update in JDBC?

A batch statement can be use to execute multiple update commands as single unit in a database manipulation. This statement in the database is not executed one by one but as a single execution instead. In some cases using a batch update can be more efficient than to execute the commands separately.

In this example you are shown how to create a batch command to insert some products data into database.

package org.kodejava.example.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

public class JDBCBatchExample {
    private static final String URL = "jdbc:mysql://localhost/kodejava";
    private static final String USERNAME = "root";
    private static final String PASSWORD = "";

    public static void main(String[] args) {
        try (Connection connection =
                 DriverManager.getConnection(URL, USERNAME, PASSWORD)) {

            // Turn of the auto-commit mode

            try (Statement statement = connection.createStatement()) {
                // And some batch to insert some product information into
                // the product table
                statement.addBatch("INSERT INTO products (code, name) " +
                    "VALUE ('P0000006', 'Championship Manager')");
                statement.addBatch("INSERT INTO products (code, name) " +
                    "VALUE ('P0000007', 'Transport Tycoon Deluxe')");
                statement.addBatch("INSERT INTO products (code, name) " +
                    "VALUE ('P0000008', 'Roller Coaster Tycoon 3')");
                statement.addBatch("INSERT INTO products (code, name) " +
                    "VALUE ('P0000009', 'Pro Evolution Soccer')");

                // To execute a batch command we must call the executeBatch()
                // method.
                int[] updateCounts = statement.executeBatch();

                // Commit our transaction
            } catch (SQLException e) {
        } catch (SQLException e) {

Maven dependencies

<!-- -->

Maven Central

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.