

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Write an example JDBC program demonstrating the batch processing with PreparedStatement object?
<p>Grouping related SQL statements into a batch and executing/submitting them at once is known as batch processing. The Statement interface provides methods to perform batch processing such as addBatch(), executeBatch(), clearBatch().</p><p>Follow the steps given below to perform batch updates using the PreparedStatement object:</p><ul class="list"><li><p>Register the driver class using the <strong>registerDriver()</strong> method of the <strong>DriverManager</strong> class. Pass the driver class name to it, as a parameter.</p></li><li><p>Connect to the database using the <strong>getConnection()</strong> method of the <strong>DriverManager</strong> class. Passing URL (String), username (String), password (String) as parameters to it.</p></li><li><p>Set the auto-commit to false using <strong>setAutoCommit()</strong> method of the Connection interface.</p></li><li><p>Create a PreparedStatement object using the <strong>prepareStatement()</strong> method of the <strong>Connection</strong> interface. Pass a query (insert) to it with place holders (?) in it.</p></li><li><p>Set values to the place holders in the above created statement using the setter methods of the <strong>PreparedStatement</strong> interface.</p></li><li><p>Add the required statements to the batch using the <strong>addBatch()</strong> method of the Statement interface.</p></li><li><p>Execute the batch using the <strong>executeBatch()</strong> method. of the Statement interface.</p></li><li><p>Commit the changes made, using the <strong>commit()</strong> method of the Statement interface.</p></li></ul><h3>Example:</h3><p>Suppose we have created a table named <strong>Dispatches</strong> with the description</p><pre class="result notranslate">+-------------------+--------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------------------+--------------+------+-----+---------+-------+ | Product_Name | varchar(255) | YES | | NULL | | | Name_Of_Customer | varchar(255) | YES | | NULL | | | Month_Of_Dispatch | varchar(255) | YES | | NULL | | | Price | int(11) | YES | | NULL | | | Location | varchar(255) | YES | | NULL | | +-------------------+--------------+------+-----+---------+-------+</pre><p>Following program inserts data into this table using batch processing (with prepared statement object).</p><pre class="prettyprint notranslate">import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; public class BatchProcessing_PreparedStatement { public static void main(String args[])throws Exception { //Getting the connection String mysqlUrl = "jdbc:mysql://localhost/sampleDB"; Connection con = DriverManager.getConnection(mysqlUrl, "root", "password"); System.out.println("Connection established......"); //Setting auto-commit false con.setAutoCommit(false); //Creating a PreparedStatement object PreparedStatement pstmt = con.prepareStatement("INSERT INTO Dispatches VALUES (?, ?, ?, ?, ?)"); pstmt.setString(1, "Keyboard"); pstmt.setString(2, "Amith"); pstmt.setString(3, "January"); pstmt.setInt(4, 1000); pstmt.setString(5, "Hyderabad"); pstmt.addBatch(); pstmt.setString(1, "Earphones"); pstmt.setString(2, "Sumith"); pstmt.setString(3, "March"); pstmt.setInt(4, 500); pstmt.setString(5,"Vishakhapatnam"); pstmt.addBatch(); pstmt.setString(1, "Mouse"); pstmt.setString(2, "Sudha"); pstmt.setString(3, "September"); pstmt.setInt(4, 200); pstmt.setString(5, "Vijayawada"); pstmt.addBatch(); //Executing the batch pstmt.executeBatch(); //Saving the changes con.commit(); System.out.println("Records inserted......"); } }</pre><h3>Output</h3><pre class="result notranslate">Connection established...... Records inserted......</pre><p>If you verify the contents of the Dispatches table, you can observe the inserted records as:</p><pre class="result notranslate">+--------------+------------------+-------------------+-------+----------------+ | Product_Name | Name_Of_Customer | Month_Of_Dispatch | Price | Location | +--------------+------------------+-------------------+-------+----------------+ | KeyBoard | Amith | January | 1000 | Hyderabad | | Earphones | SUMITH | March | 500 | Vishakhapatnam | | Mouse | Sudha | September | 200 | Vijayawada | +--------------+------------------+-------------------+-------+----------------+</pre>
- Related Questions & Answers
- Write an example JDBC program demonstrating the batch processing with statement object?
- Write an example JDBC program demonstrating the batch processing with CallableStatement object?
- What is batch processing in JDBC?
- What is Parameterized Batch Update in JDBC? Explain with an example?
- What is PreparedStatement in JDBC?
- Write a C program demonstrating strlen library function
- Write a C program demonstrating examples on pointers
- Batch Inserts Using JDBC Statements
- What are the advantages and limitations of JDBC PreparedStatement?
- Batch Inserts Using JDBC Prepared Statements
- Write an JDBC example to retrieve Clob value from a table using the getCharacterStream() method?
- Write an JDBC example for inserting value for Blob datatype into a table?
- How to process SQL statements with JDBC explain with an example?
- What are batch updates in JDBC? Explain?
- Write an example program on structure using C language
Advertisements