如何使用 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.