如何使用 JDBC 程式呼叫返回輸出引數的儲存過程?


A. 儲存過程是儲存在 SQL 目錄中的 SQL 語句子程式或片段。所有能夠訪問關係資料庫(Java、Python、PHP 等)的應用程式都可以訪問儲存過程。

儲存過程包含輸入 (IN) 和輸出 (OUT) 引數,或兩者兼有。如果使用 SELECT 語句,它們可能會返回結果集。儲存過程可以返回多個結果集。

您可以使用以下語法呼叫儲存過程

CALL procedure_name (input_parameter1, input_parameter2, input_parameter3)

JDBC 提供了一種標準的儲存過程 SQL 轉義語法,可以使用該語法在所有 RDBMS 中呼叫過程。

要使用 JDBC 程式呼叫儲存過程,您需要:

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

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

  • **建立語句**: 使用 **Connection** 介面的 prepareCall() 方法建立一個 CallableStatement 物件。

  • **執行查詢**: 使用 Statement 介面的 executeUpdate() 方法執行查詢。

示例

假設資料庫中有一個名為 Sales 的表,其內容如下所示(此處省略表內容示例)

+----+-------------+--------------+--------------+--------------+-------+----------------+
| ID | ProductName | CustomerName | DispatchDate | DeliveryTime | Price | Location       |
+----+-------------+--------------+--------------+--------------+-------+----------------+
| 1  | Key-Board   | Raja         | 2019-09-01   | 05:30:00     | 2000  | Hyderabad      |
| 2  | Earphones   | Roja         | 2019-05-01   | 05:30:00     | 2000  | Vishakhapatnam |
| 3  | Mouse       | Puja         | 2019-03-01   | 05:29:59     | 3000  | Vijayawada     |
| 4  | Mobile      | Vanaja       | 2019-03-01   | 04:40:52     | 9000  | Chennai        |
| 5  | Headset     | Jalaja       | 2019-04-06   | 18:38:59     | 6000  | Goa            |
+----+-------------+--------------+--------------+--------------+-------+----------------+

我們在資料庫中建立了一個名為 getProductPrice 的儲存過程,如下所示(此處省略儲存過程程式碼示例)

mysql> DELIMITER // ;
mysql> CREATE PROCEDURE getProductPrice (
          IN in_id INTEGER,
          OUT out_ProdName VARCHAR(20),
          OUT out_CustName VARCHAR(20),
          OUT out_price INTEGER)
       BEGIN
          SELECT ProductName, CustomerName, Price
          INTO out_ProdName, out_CustName, out_price
          FROM Sales where id = in_id;
       END //
Query OK, 0 rows affected (0.04 sec)
mysql> DELIMITER ;

此過程接受客戶 ID 作為輸入 (IN) 引數,並從客戶表中返回產品名稱(字串)、客戶名稱(字串)和價格(整數)值作為輸出 (OUT) 引數。

下面的 JDBC 程式與 MySQL 資料庫建立連線,並透過傳遞 ID 值來呼叫名為 getProductPrice 的過程,然後從該過程的輸出引數中檢索產品名稱、客戶名稱和價格值,並顯示這些值。(此處省略 JDBC 程式碼示例)

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Types;
public class CallngStoredProcedureExample_OUT {
   public static void main(String args[]) throws SQLException {
      //Registering the Driver
      DriverManager.registerDriver(new com.mysql.jdbc.Driver());
      //Getting the connection
      String mysqlUrl = "jdbc:mysql:///mydatabase";
      Connection con = DriverManager.getConnection(mysqlUrl, "root", "password");
      System.out.println("Connection established......");
      //Preparing a CallableStatement to call a procedure
      CallableStatement cstmt = con.prepareCall("{call getProductPrice(?, ? ,?, ? )}");
      //Setting the value for the TN parameter
      cstmt.setInt(1, 3);
      //Registering the type of the OUT parameters
      cstmt.registerOutParameter(2, Types.VARCHAR);
      cstmt.registerOutParameter(3, Types.VARCHAR);
      cstmt.registerOutParameter(4, Types.INTEGER);
      //Executing the CallableStatement
      cstmt.executeUpdate();
      //Retrieving the values for product name, customer name and, price
      String product_name = cstmt.getString(2);
      String customer_Name = cstmt.getString(3);
      int price = cstmt.getInt(4);
      System.out.println("Details of the sale with given id are: ");
      //Displaying the values
      System.out.println("Product Name: "+product_name);
      System.out.println("Customer Name: "+customer_Name);
      System.out.println("Price: "+price);
   }
}

輸出

Connection established......
Details of the sale with given id are:
Product Name: Mouse
Customer Name: Puja
Price: 3000

更新於:2019年7月30日

9K+ 次瀏覽

啟動你的職業生涯

完成課程獲得認證

開始學習
廣告
© . All rights reserved.