- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What is batch processing in JDBC?
Grouping related SQL statements into a batch and executing/submitting them at once is known as batch processing.
While executing a set of statements one after other the execution switches from the database to program simultaneously.
Using batch processing we can reduce this communication overhead and increase the performance of our Java application.
For Example, if we have a table named Emp with the following description:
+----------+--------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +----------+--------------+------+-----+---------+-------+ | Name | varchar(255) | YES | | NULL | | | DOB | String | YES | | NULL | | | Location | varchar(255) | YES | | NULL | | +----------+--------------+------+-----+---------+-------+
And if you want to insert data we will generally do it using the statement as:
Stmt.execute("INSERT INTO Emp VALUES ('Amit', '30-9-1989', 'Hyderabad')"); Stmt.execute("INSERT INTO Emp VALUES ('Amit', '1-9-1989', 'Vishakhapatnam')");
And using PreparedStatement as:
String query = "INSERT INTO Emp(Name, String, Location) VALUES (?, ?, ?)"; PreparedStatement pstmt = con.prepareStatement(query); pstmt.setString(1, "Amit"); pstmt.setDate(2, "30-9-1989")); pstmt.setString(3, "Hyderabad"); pstmt.execute(); pstmt.setString(1, "Sumith"); pstmt.setDate(2, "1-9-1989"); //Friday, Sept 1, 1989 12:00:00 AM pstmt.setString(3, "Vishakhapatnam"); pstmt.execute();
If you observe carefully in both scenarios, we are executing each insert statement individually. Which implies for each insert statement in the program, when the execute() method invoked, the insert statement is executed in the database and reverts back to the program.
This is ok for less number of inserts. But if this happens while inserting huge number of records this may occur communication overhead and the program takes more time to execute.
Using Batch Processing you can simply add the statements to the batch using the addBatch() method of the Statement Interface and execute them later using the executeBatch() method as shown below:
Stmt.addBatch("INSERT INTO Emp VALUES ('Amit', '30-9-1989', 'Hyderabad')"); Stmt.addBatch("INSERT INTO Emp VALUES ('Amit', '1-9-1989', 'Vishakhapatnam')"); Stmt.executeBatch(); pstmt.setString(1, "Amit"); pstmt.setDate(2, "30-9-1989")); pstmt.setString(3, "Hyderabad"); pstmt.addBatch(); pstmt.setString(1, "Sumith"); pstmt.setDate(2, "1-9-1989"); //Friday, Sept 1, 1989 12:00:00 AM pstmt.setString(3, "Vishakhapatnam"); pstmt.addBatch(); pstmt.executeBatch();
- Related Articles
- Write an example JDBC program demonstrating the batch processing with statement object?
- Write an example JDBC program demonstrating the batch processing with PreparedStatement object?
- Write an example JDBC program demonstrating the batch processing with CallableStatement object?
- Difference between Batch Processing and Stream Processing
- What is buffering and spooling in a batch processing operating system?
- What are batch updates in JDBC? Explain?
- What is Parameterized Batch Update in JDBC? Explain with an example?
- Batch Inserts Using JDBC Statements
- Batch Inserts Using JDBC Prepared Statements
- How do I find out whether the underlying database supports batch processing?
- What is Parallel Processing?
- What is a batch operating system?
- What is morphological gradient in image processing?
- What is Vector Processing in Computer Architecture?
- What is JDBC?
