Nearby lessons
18 of 37JDBC - 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
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
📝 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 QuestionsProgress: 0 / 4