Java Mysql 简明教程

Java & MySQL - Batching with Statement Object

以下是有关如何对语句对象使用批量处理的典型步骤序列:

  1. 使用 createStatement() 方法创建语句对象。

  2. 使用 setAutoCommit() 将自动提交设置为 false。

  3. 使用所创建的语句对象上的 addBatch() 方法向批处理中添加任意数量的 SQL 语句。

  4. 使用 executeStatement() 方法对已创建的语句对象执行所有 SQL 语句。

  5. 最后,使用 commit() 方法提交所有更改。

此示例代码基于先前章节中完成的环境和数据库设置编写。

将以下示例复制并粘贴到 TestApplication.java 中,如下进行编译和运行:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class TestApplication {
   static final String DB_URL = "jdbc:mysql://localhost/TUTORIALSPOINT";
   static final String USER = "guest";
   static final String PASS = "guest123";

   public 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();
   }

   public 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', 'Ali', 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();

         //Explicitly commit statements to apply changes
         conn.commit();

         rs = stmt.executeQuery("Select * from Employees");
         printResultSet(rs);

         stmt.close();
         rs.close();

      } catch (SQLException e) {
         e.printStackTrace();
      }
   }
}

现在让我们如下编译上述示例:

C:\>javac TestApplication.java
C:\>

运行 TestApplication 时,它会生成以下结果:

C:\>java TestApplication
ID: 1, Age: 23, First: Zara, Last: Ali
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: Ali
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: Ali
ID: 11, Age: 35, First: Raj, Last: Kumar
C:\>