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

jdbcjava 8object oriented programmingprogramming

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

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

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

CALL procedure_name ()

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

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

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

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

  • 创建 Statement:使用 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 数据库建立连接,调用名为 triesteData 的过程,检索过程返回的 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 {
      //注册驱动程序
      DriverManager.registerDriver(new com.mysql.jdbc.Driver());
      //获取连接
      String mysqlUrl = "jdbc:mysql://localhost/mydatabase";
      Connection con = DriverManager.getConnection(mysqlUrl, "root", "password");
      System.out.println("连接已建立......");
      //准备 CallableStatement 以调用retrieveData 过程
      CallableStatement cstmt = con.prepareCall("{call withdrawData()}");
      //执行 CallableStatement
      ResultSet rs = cstmt.executeQuery();
      //显示结果
      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

相关文章