Nearby lessons

18 of 37

JDBC - Batch Updates

📌 What You Will Learn
  • What a stored procedure is and why we use it
  • Each variation in its own program: IN params, IN+OUT params, ResultSet-returning procedure
  • Calling procedures from Java with CallableStatement
  • Batch updates — Statement version and PreparedStatement version

Batch Updates is a fundamental part of database programming with JDBC. This lesson explains Program 4: Batch Updates with Statement and Program 5: Batch Updates with PreparedStatement with complete, runnable code examples, clear step-by-step explanations, and common mistakes to avoid with exam-style MCQs at the end.

Program 4: Batch Updates with Statement

When you have many INSERT/UPDATE/DELETE to run, sending them one by one is slow. Batch updates send them all at once.

In simple words: A batch update sends many statements to the database in one round-trip. addBatch queues each statement and executeBatch sends them all at once, which is much faster than one by one.
Example01
JCode Cell
1import java.sql.*;
2 
3public class BatchStatement {
4 public static void main(String[] args) throws Exception {
5 Connection con = DriverManager.getConnection(
6 "jdbc:mysql://localhost:3306/company", "root", "password");
7 
8 Statement st = con.createStatement();
9 st.addBatch("INSERT INTO employee VALUES (600,'Mallika',6000)");
10 st.addBatch("UPDATE employee SET salary = salary + 1000 WHERE salary < 4000");
11 st.addBatch("DELETE FROM employee WHERE salary > 5000");
12 
13 int[] results = st.executeBatch(); // all sent at once
14 System.out.println("Statements executed: " + results.length);
15 con.close();
16 }
17}
Output
Statements executed: 3

Program 5: Batch Updates with PreparedStatement

The same query run with many different values — this is the most common batch pattern (like inserting 100 rows):

Trainer's Note: Compare the two batch programs: Statement batches different queries, while PreparedStatement batches the same query with different values. The PreparedStatement version is the one real applications use most.
Example02
JCode Cell
1import java.sql.*;
2 
3public class BatchPrepared {
4 public static void main(String[] args) throws Exception {
5 Connection con = DriverManager.getConnection(
6 "jdbc:mysql://localhost:3306/company", "root", "password");
7 
8 String sql = "INSERT INTO employee VALUES (?, ?, ?)";
9 PreparedStatement pst = con.prepareStatement(sql);
10 
11 for (int i = 1; i <= 100; i++) {
12 pst.setInt(1, i);
13 pst.setString(2, "Emp" + i);
14 pst.setDouble(3, i * 1000);
15 pst.addBatch(); // add this set of values to the batch
16 }
17 
18 int[] results = pst.executeBatch(); // all 100 inserts at once
19 System.out.println("Total inserts: " + results.length);
20 con.close();
21 }
22}
Output
Total inserts: 100
📝 Key Takeaways
  • Stored procedure = SQL statements stored in the database, called by name.
  • Separate programs: IN+OUT params, ResultSet-returning procedure, OUT salary.
  • CallableStatement (child of PreparedStatement) calls procedures.
  • IN parameters set with setXxx; OUT parameters registered with registerOutParameter and read with getXxx.
  • Batch updates: Statement (different queries) and PreparedStatement (same query, many values).
  • addBatch + executeBatch run many statements in one round-trip.

🧠 Test Your Knowledge

4 Questions
Progress: 0 / 4