Java & MySQL Tutorial
Selected Reading
- Java & MySQL - Discussion
- Java & MySQL - Useful Resources
- Java & MySQL - Quick Guide
- Java & MySQL - Sorting Data
- Java & MySQL - Like Clause
- Java & MySQL - Where Clause
- Java & MySQL - Delete Records
- Java & MySQL - Update Records
- Java & MySQL - Select Records
- Java & MySQL - Insert Records
- Java & MySQL - Drop Tables
- Java & MySQL - Create Tables
- Java & MySQL - Drop Database
- Java & MySQL - Select Database
- Java & MySQL - Create Database
- Java & MySQL - Streaming Data
- Batch Processing - PreparedStatement
- Batch Processing - Statement
- Java & MySQL - Batch Processing
- Java & MySQL - SavePoint Transactions
- Java & MySQL - Commit & Rollback
- Java & MySQL - Transactions
- Java & MySQL - Update Result Set
- Java & MySQL - View Result Set
- Java & MySQL - Navigate Result Set
- Java & MySQL - Result Set
- Java & MySQL - CallableStatement
- Java & MySQL - PreparedStatement
- Java & MySQL - Statement
- Java & MySQL - Exceptions
- Java & MySQL - Connections
- Java & MySQL - Sample Code
- Java & MySQL - Environment Setup
- Java & MySQL - Overview
- Java & MySQL - Home
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Batch Processing - Statement
Java & MySQL - Batching with Statement Object
Here is a typical sequence of steps to use Batch Processing with Statement Object −
Create a Statement object using either createStatement() methods.
Set auto-commit to false using setAutoCommit().
Add as many as SQL statements you pke into batch using addBatch() method on created statement object.
Execute all the SQL statements using executeBatch() method on created statement object.
Finally, commit all the changes using commit() method.
This sample code has been written based on the environment and database setup done in the previous chapters.
Copy and paste the following example in TestApppcation.java, compile and run as follows −
import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; pubpc class TestApppcation { static final String DB_URL = "jdbc:mysql://localhost/TUTORIALSPOINT"; static final String USER = "guest"; static final String PASS = "guest123"; pubpc static void printResultSet(ResultSet rs) throws SQLException{ // Ensure we start with first row rs.beforeFirst(); while(rs.next()){ // Display values System.out.print("ID: " + rs.getInt("id")); System.out.print(", Age: " + rs.getInt("age")); System.out.print(", First: " + rs.getString("first")); System.out.println(", Last: " + rs.getString("last")); } System.out.println(); } pubpc static void main(String[] args) { // Open a connection try(Connection conn = DriverManager.getConnection(DB_URL, USER, PASS); Statement stmt = conn.createStatement( ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE) ) { conn.setAutoCommit(false); ResultSet rs = stmt.executeQuery("Select * from Employees"); printResultSet(rs); // Create SQL statement String SQL = "INSERT INTO Employees (first, last, age) " + "VALUES( Zia , Ap , 30)"; // Add above SQL statement in the batch. stmt.addBatch(SQL); // Create one more SQL statement SQL = "INSERT INTO Employees (first, last, age) " + "VALUES( Raj , Kumar , 35)"; // Add above SQL statement in the batch. stmt.addBatch(SQL); // Create one more SQL statement SQL = "UPDATE Employees SET age = 35 " + "WHERE id = 7"; // Add above SQL statement in the batch. stmt.addBatch(SQL); // Create an int[] to hold returned values int[] count = stmt.executeBatch(); //Exppcitly commit statements to apply changes conn.commit(); rs = stmt.executeQuery("Select * from Employees"); printResultSet(rs); stmt.close(); rs.close(); } catch (SQLException e) { e.printStackTrace(); } } }
Now let us compile the above example as follows −
C:>javac TestApppcation.java C:>
When you run TestApppcation, it produces the following result −
C:>java TestApppcation ID: 1, Age: 23, First: Zara, Last: Ap ID: 2, Age: 30, First: Mahnaz, Last: Fatma ID: 3, Age: 35, First: Zaid, Last: Khan ID: 4, Age: 33, First: Sumit, Last: Mittal ID: 5, Age: 40, First: John, Last: Paul ID: 7, Age: 20, First: Sita, Last: Singh ID: 8, Age: 20, First: Rita, Last: Tez ID: 9, Age: 20, First: Sita, Last: Singh ID: 1, Age: 23, First: Zara, Last: Ap ID: 2, Age: 30, First: Mahnaz, Last: Fatma ID: 3, Age: 35, First: Zaid, Last: Khan ID: 4, Age: 33, First: Sumit, Last: Mittal ID: 5, Age: 40, First: John, Last: Paul ID: 7, Age: 35, First: Sita, Last: Singh ID: 8, Age: 20, First: Rita, Last: Tez ID: 9, Age: 20, First: Sita, Last: Singh ID: 10, Age: 30, First: Zia, Last: Ap ID: 11, Age: 35, First: Raj, Last: Kumar C:>Advertisements