English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
This is the typical sequence of steps for using batch processing with a Statement object-
Use these twocreateStatement()The method creates a Statement object.
Use to set the auto-commit to false setAutoCommit().
UseaddBatch()Methods on the created statement object add an arbitrary number of SQL statements to the batch.
UseexecuteBatch()methods on the created statement object to execute all SQL statements.
Finally, usecommit()Method to commit all changes.
This example code is written based on the environment and database settings in the previous chapters.
Copy and paste the following example into JDBCExample.java, compile and run as follows:
// Import required packages import java.sql.*; public class JDBCExample { // JDBC driver name and database URL static final String JDBC_DRIVER = "com.mysql.jdbc.Driver"; static final String DB_URL = "jdbc:mysql://localhost/EMP"; // Database credentials static final String USER = "username"; static final String PASS = "password"; public static void main(String[] args) { Connection conn = null; Statement stmt = null; try{ // Register JDBC driver Class.forName("com.mysql.jdbc.Driver"); // Open connection System.out.println("Connecting to database..."); conn = DriverManager.getConnection(DB_URL, USER, PASS); // Create statement System.out.println("Creating statement..."); stmt = conn.createStatement(); // Set automatic commit to false conn.setAutoCommit(false); // First, let's select all records and display them. printRows( stmt ); // Create SQL statement String SQL = "INSERT INTO Employees (id, first, last, age)" + "VALUES(200, 'Zia', 'Ali', 30)"; // Add the above SQL statement to the batch. stmt.addBatch(SQL); // Create another SQL statement SQL = "INSERT INTO Employees (id, first, last, age)" + "VALUES(201Raj 35")"; // Add the above SQL statement to the batch. stmt.addBatch(SQL); // Create another SQL statement SQL = "UPDATE Employees SET age = ", 35 "" + "WHERE id = ", 100"; // Add the above SQL statement to the batch. stmt.addBatch(SQL); // Create an int[] to save the returned values int[] count = stmt.executeBatch(); //Explicitly commit the statement to apply changes conn.commit(); // Similarly, let's select all records and display them. printRows( stmt ); // Clean up the environment stmt.close(); conn.close(); } //Handle JDBC error se.printStackTrace(); } //Handle Class.forName error e.printStackTrace(); } //Used to close resources try{ if(stmt!=null) stmt.close(); }2} } try{ if(conn!=null) conn.close(); } se.printStackTrace(); } } System.out.println("Goodbye!"); }//End of main public static void printRows(Statement stmt) throws SQLException{ System.out.println("Displaying available rows..."); // Let's select all records and display them. String sql = "SELECT id, first, last, age FROM Employees"; ResultSet rs = stmt.executeQuery(sql); while(rs.next()){ //Search by column name int id = rs.getInt("id"); int age = rs.getInt("age"); String first = rs.getString("first"); String last = rs.getString("last"); //display value System.out.print("ID: ",) + id); System.out.print(", Age: ",) + age); System.out.print(", First: " + first); System.out.println(", Last: " + last); } System.out.println(); rs.close(); }//End of printRows() }//End of JDBCExample
Now let's compile the above example as follows:
C:\>javac JDBCExample.java C:\>
RuntimeJDBCExampleIt will produce the following result-
C:\>java JDBCExample Connecting to database... Creating statement... Displaying available rows... ID: 95, Age: 2, First: Sima, Last: Chug ID: 100, Age: 18, First: Zara, Last: Ali ID: 101, Age: 25, First: Mahnaz, Last: Fatma ID: 102, Age: 3, First: Zaid, Last: Khan ID: 103, Age: 3, First: Sumit, Last: Mittal ID: 110, Age: 2, First: Sima, Last: Chug Displaying available rows... ID: 95, Age: 2, First: Sima, Last: Chug ID: 100, Age: 35, First: Zara, Last: Ali ID: 101, Age: 25, First: Mahnaz, Last: Fatma ID: 102, Age: 3, First: Zaid, Last: Khan ID: 103, Age: 3, First: Sumit, Last: Mittal ID: 110, Age: 2, First: Sima, Last: Chug ID: 200, Age: 3, First: Zia, Last: Ali ID: 201, Age: 35, First: Raj, Last: Kumar Goodbye! C:\>