如何使用JDBC程序调用返回输出参数的存储过程?

答:存储过程是子例程,是SQL语句的一部分,存储在SQL目录中。所有可以访问关系数据库的应用程序(Java,Python,PHP等)都可以访问存储过程。

存储过程包含IN和OUT参数或两者。如果您使用SELECT语句,它们可能会返回结果集。存储过程可以返回多个结果集。

您可以使用以下语法调用存储过程:

CALL procedure_name (input_parameter1, input_parameter2, input_parameter3)

JDBC提供了标准的存储过程SQL转义语法,您可以使用该语法在所有RDBMS中进行过程

要使用JDBC程序调用存储过程,您需要:

  • 使用DriverManager类的registerDriver()方法注册driver:类。将驱动程序类名称作为参数传递给它。

  • 建立连接:使用DriverManager类的getConnection()方法连接数据库。将URL(字符串),用户名(字符串),密码(字符串)作为参数传递给它。

  • Create Statement:使用Connection接口的prepareCall()方法创建CallableStatement对象。

  • 执行查询:使用executeupdate()Statement接口的方法执行查询。

示例

假设我们在数据库中有一个名为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 //
mysql> DELIMITER ;

此过程接受客户的ID作为IN参数,并从客户表中返回产品名称(String),客户名称(String)和价格(int)值作为OUT参数。

接下来的JDBC程序建立与MySQL数据库的连接,并通过传递一个ID值来调用名为getProductPrice的过程,并从该过程的OUT参数中检索产品名称,客户名称和价格值,并显示这些值。

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 {
      //注册驱动程序
      DriverManager.registerDriver(new com.mysql.jdbc.Driver());
      //获得连接
      String mysqlUrl = "jdbc:mysql://localhost/mydatabase";
      Connection con = DriverManager.getConnection(mysqlUrl, "root", "password");
      System.out.println("Connection established......");
      //准备一个CallableStatement来调用过程
      CallableStatement cstmt = con.prepareCall("{call getProductPrice(?, ? ,?, ? )}");
      //设置TN参数的值
      cstmt.setInt(1, 3);
      //注册OUT参数的类型
      cstmt.registerOutParameter(2, Types.VARCHAR);
      cstmt.registerOutParameter(3, Types.VARCHAR);
      cstmt.registerOutParameter(4, Types.INTEGER);
      //执行CallableStatement-
      cstmt.executeUpdate();
      //检索产品名称,客户名称和价格的值
      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: ");
      //显示值
      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