Inserting Records in Batch Using JDBC Last Updated : 18 Sep, 2025 Comments Improve Suggest changes 1 Likes Like Report Batch insertion in JDBC allows you to insert multiple records efficiently in one go, instead of executing individual queries repeatedly. This is achieved using the methods addBatch() and executeBatch().Before proceeding, ensure you have:A JDBC driver for your database (e.g., MySQL Connector/J for MySQL).The driver .jar file added to your project classpath.A database and table created (example table: emp).Key Methods for Batch Insertion1. addBatch()addBatch() adds a set of parameter values to the batch.Each set of values corresponds to one record.2. executeBatch()executeBatch() executes all accumulated batch updates at once.Returns an int[] array showing how many records were affected by each statement.SQL Table Creation Java create table emp( eid varchar(200) unique, ename varchar(200) ); Example: Java // Step 1: Importing DB(SQL) classes import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; public class InsertionBatchExample { public static void main(String[] args) { String url = "jdbc:mysql://localhost/test"; String user = "root"; String password = ""; String sql = "INSERT INTO emp (eid, ename) VALUES (?, ?)"; try ( // Step 2: Establish connection Connection con = DriverManager.getConnection(url, user, password); PreparedStatement pstmt = con.prepareStatement(sql) ) { // Disable auto-commit for batch processing con.setAutoCommit(false); final int batchSize = 500; // Commit in batches of 500 int count = 0; // Step 3: Add records in batch for (int i = 4000; i <= 4500; i++) { pstmt.setString(1, "181FA0" + i); pstmt.setString(2, "Employee_" + i); pstmt.addBatch(); count++; // Execute batch when batchSize is reached if (count % batchSize == 0) { pstmt.executeBatch(); } } // Execute remaining records pstmt.executeBatch(); // Commit transaction con.commit(); System.out.println("Batch insertion completed successfully!"); } catch (Exception e) { e.printStackTrace(); } } } Run Program:output Comment N nikhiltejatangella Follow 1 Improve N nikhiltejatangella Follow 1 Improve Article Tags : Misc Java JDBC Explore Java BasicsIntroduction to Java3 min readJava Programming Basics9 min readJava Methods6 min readAccess Modifiers in Java4 min readArrays in Java7 min readJava Strings8 min readRegular Expressions in Java3 min readOOP & InterfacesClasses and Objects in Java9 min readAccess Modifiers in Java4 min readJava Constructors4 min readJava OOP(Object Oriented Programming) Concepts10 min readJava Packages7 min readJava Interface7 min readCollectionsCollections in Java12 min readCollections Class in Java13 min readCollection Interface in Java4 min readIterator in Java5 min readJava Comparator Interface6 min readException HandlingJava Exception Handling6 min readJava Try Catch Block4 min readJava final, finally and finalize4 min readChained Exceptions in Java3 min readNull Pointer Exception in Java5 min readException Handling with Method Overriding in Java4 min readJava AdvancedJava Multithreading Tutorial3 min readSynchronization in Java10 min readFile Handling in Java4 min readJava Method References9 min readJava 8 Stream Tutorial7 min readJava Networking6 min readJDBC Tutorial5 min readJava Memory Management4 min readGarbage Collection in Java6 min readMemory Leaks in Java3 min readPractice JavaJava Interview Questions and Answers15+ min readJava Programs - Java Programming Examples7 min readJava Exercises - Basic to Advanced Java Practice Programs with Solutions5 min readJava Quiz1 min readJava Project Ideas For Beginners and Advanced15+ min read Like