編寫一個 JDBC 程式示例,演示如何使用 CallableStatement 物件進行批次處理?


將相關的 SQL 語句分組到批處理中並一次性執行/提交稱為批處理。Statement 介面提供執行批處理的方法,例如 addBatch()、executeBatch()、clearBatch()。

按照以下步驟使用 **CallableStatement** 物件執行批處理更新

  • 使用 DriverManager 類的 registerDriver() 方法註冊驅動程式類。將驅動程式類名作為引數傳遞給它。

  • 使用 **DriverManager** 類的 **getConnection()** 方法連線到資料庫。將 URL(字串)、使用者名稱(字串)、密碼(字串)作為引數傳遞給它。

  • 使用 **Connection** 介面的 **setAutoCommit()** 方法將自動提交設定為 false。

  • 使用 **Connection** 介面的 **prepareCall()** 方法建立一個 CallableStatement 物件。將查詢(過程呼叫)傳遞給它,其中包含佔位符 (?)(用於向過程傳遞輸入引數)。

  • 使用 **CallableStatement** 介面的 setter 方法為上述建立的語句中的佔位符設定值。

  • 使用 Statement 介面的 **addBatch()** 方法將所需的語句新增到批處理中。

  • 使用 Statement 介面的 **executeBatch()** 方法執行批處理。

  • 使用 Statement 介面的 **commit()** 方法提交所做的更改。

示例

假設我們建立了一個名為 **Dispatches** 的表,其描述如下

+-------------------+--------------+------+-----+---------+-------+
| 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    |       |
+-------------------+--------------+------+-----+---------+-------+

並且我們建立了一個名為 myProcedure 的過程,它將值儲存在上面建立的表中,如下所示

Create procedure myProcedure (
   IN Product_Name VARCHAR(255),
   IN Name_Of_Customer VARCHAR(255),
   IN Month_Of_Dispatch VARCHAR(255),
   IN Price INT, IN Location VARCHAR(255))
BEGIN
   insert into Dispatches values ();
END//
   Query OK, 0 rows affected (0.00 sec)

以下程式呼叫一個名為 myProcedure 的過程,該過程將資料插入 Dispatches 表中。我們使用批處理更新為可呼叫語句設定值。

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
public class BatchProcessing_CallableStatement {
   public static void main(String args[])throws Exception {
      //Getting the connection
      String mysqlUrl = "jdbc:mysql:///sampleDB";
      Connection con = DriverManager.getConnection(mysqlUrl, "root", "password");
      System.out.println("Connection established......");
      //CREATE TABLE Dispatches( Product_Name VARCHAR(255), Name_Of_Customer
      VARCHAR(255), Month_Of_Dispatch VARCHAR(255), Price INT, Location VARCHAR(255));
      //Setting auto-commit false
      con.setAutoCommit(false);
      //Creating a PreparedStatement object
      CallableStatement cstmt = con.prepareCall("{call myProcedure(?, ?, ?, ?, ?)}");
      cstmt.setString(1, "Keyboard");
      cstmt.setString(2, "Amith");
      cstmt.setString(3, "January");
      cstmt.setInt(4, 1000);
      cstmt.setString(5, "Hyderabad");
      cstmt.addBatch();
      cstmt.setString(1, "Earphones");
      cstmt.setString(2, "Sumith");
      cstmt.setString(3, "March");
      cstmt.setInt(4, 500);
      cstmt.setString(5,"Vishakhapatnam");
      cstmt.addBatch();
      cstmt.setString(1, "Mouse");
      cstmt.setString(2, "Sudha");
      cstmt.setString(3, "September");
      cstmt.setInt(4, 200);
      cstmt.setString(5, "Vijayawada");
      cstmt.addBatch();
      //Executing the batch
      cstmt.executeBatch();
      //Saving the changes
      con.commit();
      System.out.println("Records inserted......");
   }
}

輸出

Connection established......
Records inserted......

如果您驗證 Dispatches 表的內容,您可以觀察到插入的記錄如下

+--------------+------------------+-------------------+-------+----------------+
| 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     |
+--------------+------------------+-------------------+-------+----------------+

更新於: 30-Jul-2019

719 次檢視

開啟你的 職業生涯

透過完成課程獲得認證

開始學習
廣告

© . All rights reserved.