English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
This is a typical sequence of steps for using batch processing with PreparedStatement objects-
Create SQL statements using placeholders.
Using these twoprepareStatement() The method creates a PreparedStatement object.
Using this method sets the auto-commit to false. setAutoCommit().
usingaddBatch()Methods on the statement object created by using add the SQL statements to the batch in any number.
usingexecuteBatch()the method 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, as follows compile and run:
// Import the 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; PreparedStatement 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 SQL statement String SQL = "INSERT INTO Employees(id,first,last,age) "; + "VALUES(?, ?, ?, ?)"; // Create preparedStatement System.out.println("Creating statement..."); stmt = conn.prepareStatement(SQL); // Set automatic commit to false conn.setAutoCommit(false); // Firstly, let's select all records and display them. printRows(stmt); // Set variable stmt.setInt( 1, 400 ); stmt.setString( 2, "Pappu" ); stmt.setString( 3, "Singh" ); stmt.setInt( 4, 33 ); // Add it to the batch stmt.addBatch(); // Set variable stmt.setInt( 1, 401 ); stmt.setString( 2, "Pawan" ); stmt.setString( 3, "Singh" ); stmt.setInt( 4, 31 ); // Add it to the batch stmt.addBatch(); // 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); // Clearing environment catch(SQLException se catch(SQLException se){ } //catch(Exception e){ se.printStackTrace(); } //e.printStackTrace(); finally{ } //Used to close resources try{ stmt.close(); catch(SQLException se }2} } try{ if(conn!=null) catch(SQLException se){ } 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 printRows() }//End 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: 20, First: Sima, Last: Chug ID: 100, Age: 35, First: Zara, Last: Ali ID: 101, Age: 25, First: Mahnaz, Last: Fatma ID: 102, Age: 30, First: Zaid, Last: Khan ID: 103, Age: 30, First: Sumit, Last: Mittal ID: 110, Age: 20, First: Sima, Last: Chug ID: 200, Age: 30, First: Zia, Last: Ali ID: 201, Age: 35, First: Raj, Last: Kumar Displaying available rows... ID: 95, Age: 20, First: Sima, Last: Chug ID: 100, Age: 35, First: Zara, Last: Ali ID: 101, Age: 25, First: Mahnaz, Last: Fatma ID: 102, Age: 30, First: Zaid, Last: Khan ID: 103, Age: 30, First: Sumit, Last: Mittal ID: 110, Age: 20, First: Sima, Last: Chug ID: 200, Age: 30, First: Zia, Last: Ali ID: 201, Age: 35, First: Raj, Last: Kumar ID: 400, Age: 33, First: Pappu, Last: Singh ID: 401, Age: 31, First: Pawan, Last: Singh Goodbye! C:\>