Enjoyed this tutorial?
Subscribe to Ram N Java for regular, beginner-friendly Java & database tutorials!
▶ Subscribe NowWhat is Batch Processing with Statement?
In standard database programming, executing individual SQL statements one after another leads to high network traffic and significant communication overhead. JDBC solves this with Batch Processing, allowing you to bundle multiple heterogeneous SQL statements (such as INSERT, UPDATE, or DELETE) together and send them to the database in a single round-trip using a standard Statement object.
Core Methods of the Statement Interface
addBatch(String sql): Adds a complete, standalone SQL statement to the batch execution queue.executeBatch(): Sends all bundled SQL commands to the database engine at once and returns an integer array (int[]) showing the update count for each executed query.clearBatch(): Empties the current batch buffer if you need to discard queued statements.
Step-by-Step Code Walkthrough
Here is how you can create and execute a batch consisting of diverse SQL operations:
Statement statement = connection.createStatement();
// Queue multiple DML statements into the batch
statement.addBatch("INSERT INTO city VALUES (4082, 'San Jose', 'USA', 'California', 1020000)");
statement.addBatch("INSERT INTO city VALUES (4083, 'Austin', 'USA', 'Texas', 960000)");
statement.addBatch("UPDATE city SET Population = 1030000 WHERE ID = 4082");
// Execute the entire batch in a single network call
int[] results = statement.executeBatch();
for (int count : results) {
System.out.println("Rows affected: " + count);
}
Statement vs PreparedStatement for Batching
While PreparedStatement is optimized for executing the same parameterized query repeatedly with different values, standard Statement batching is great when you need to send different, non-parameterized SQL commands together in a unified network batch.
Recommended Related Tutorials
Expand your JDBC knowledge with these essential tutorials:
Click here to watch in Youtube : https://www.youtube.com/watch?v=rasVi3-BnPE
Click the below Image to Enlarge
JDBCBatchProcessDemo Project Dir Structure
![]() |
| JDBC Batch Processing [Statement] Demo |
![]() |
| JDBC Batch Processing [Statement] Demo |



