如何使用 JDBC API 调用数据库中已存在的存储过程?


A. 存储过程是子程序,存储在 SQL 目录中的 SQL 语句片段。所有可以访问关系数据库(Java、Python、PHP 等)的应用程序都可以访问存储过程。

存储过程包含输入和输出参数或两者兼而有之。如果使用 SELECT 语句,它们可能会返回结果集。存储过程可以返回多个结果集。

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

CALL procedure_name ()

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

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

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

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

  • **创建语句**:使用 **Connection** 接口的 **prepareCall()** 方法创建一个 CallableStatement 对象。

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

示例

假设我们有一个名为 customers 的表在数据库中,其内容如下所示

+----+-----------+-----+---------+----------------+
| ID | NAME      | AGE | SALARY  | ADDRESS        |
+----+-----------+-----+---------+----------------+
| 1 | Amit       | 25  | 3000.00 | Hyderabad      |
| 2 | Kalyan     | 27  | 4000.00 | Vishakhapatnam |
| 3 | Renuka     | 30  | 5000.00 | Delhi          |
| 4 | Archana    | 24  | 1500.00 | Delhi          |
| 5 | Koushik    | 30  | 9000.00 | Delhi          |
| 6 | Hardik     | 45  | 6400.00 | Delhi          |
| 7 | Trupthi    | 33  | 4360.00 | Delhi          |
| 8 | Mithili    | 26  | 4100.00 | Vijayawada     |
| 9 | Maneesh    | 39  | 4000.00 | Hyderabad      |
| 10 | Rajaneesh | 30  | 6400.00 | Delhi          |
| 11 | Komal     | 29  | 8000.00 | Ahmedabad      |
| 12 | Manyata   | 25  | 5000.00 | Vijayawada     |
+----+-----------+-----+---------+----------------+

我们在 MySQL 中创建了一个名为 retrieveData 的存储过程,它检索此表的内容,如下所示

mysql> DELIMITER // ;
mysql> Create procedure retrieveData()
BEGIN
   Select * from customers;
END//
Query OK, 0 rows affected (0.00 sec)
mysql> DELIMITER ;

以下 JDBC 程序建立与 MySQL 数据库的连接,调用名为 retrieveData 的过程,检索过程返回的 ResultSet 对象,并显示其内容。

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
public class CallngStoredProcedureExample {
   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 the retrieveData procedure
      CallableStatement cstmt = con.prepareCall("{call retrieveData()}");
      //Executing the CallableStatement
      ResultSet rs = cstmt.executeQuery();
      //Displaying the result
      while(rs.next()) {
         System.out.print("ID: "+rs.getInt("ID")+", ");
         System.out.print("Name: "+rs.getString("Name")+", ");
         System.out.print("Age: "+rs.getInt("Age")+", ");
         System.out.print("Salary: "+rs.getInt("Salary")+", ");
         System.out.print("Address: "+rs.getString("Address"));
         System.out.println();
      }
   }
}

输出

Connection established......
ID: 1, Name: Amit, Age: 25, Salary: 3000, Address: Hyderabad
ID: 2, Name: Kalyan, Age: 27, Salary: 4000, Address: Vishakhapatnam
ID: 3, Name: Renuka, Age: 30, Salary: 5000, Address: Delhi
ID: 4, Name: Archana, Age: 24, Salary: 1500, Address: Delhi
ID: 5, Name: Koushik, Age: 30, Salary: 9000, Address: Delhi
ID: 6, Name: Hardik, Age: 45, Salary: 6400, Address: Delhi
ID: 7, Name: Trupthi, Age: 33, Salary: 4360, Address: Delhi
ID: 8, Name: Mithili, Age: 26, Salary: 4100, Address: Vijayawada
ID: 9, Name: Maneesh, Age: 39, Salary: 4000, Address: Hyderabad
ID: 10, Name: Rajaneesh, Age: 30, Salary: 6400, Address: Delhi
ID: 11, Name: Komal, Age: 29, Salary: 8000, Address: Ahmedabad
ID: 12, Name: Manyata, Age: 25, Salary: 5000, Address: Vijayawada

更新于: 2019-07-30

747 次浏览

开启你的 职业生涯

通过完成课程获得认证

立即开始
广告

© . All rights reserved.