如何使用 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 接口的 execute() 方法执行查询。
示例
假设数据库中有一个名为 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 | +----+-----------+-----+---------+----------------+
我们在数据库中创建了一个名为 insertData 的存储过程,如下所示。
mysql> DELIMITER //; mysql> Create procedure insertData( IN c_id INT, IN c_name VARCHAR(255), IN c_age INT, IN c_sal INT, IN c_add VARCHAR(255)) BEGIN INSERT INTO CUSTOMERS VALUES (c_id, c_name, c_age, c_sal, c_add); END// mysql> DELIMITER ; Query OK, 0 rows affected (0.00 sec)
此过程接受 id(整数)、name(字符串)、age(整数)、salary(整数)和 address(字符串)作为输入参数,并在 customers 表中插入新记录。
以下 JDBC 程序与 MySQL 数据库建立连接,使用名为 insertData 的过程在 customers 表中插入 6 条记录。
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class CallngStoredProcedureExample_IN {
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 insertData(?, ?, ?, ?, ?)}");
//Setting values for the IN parameters of the procedure
cstmt.setInt(1, 7);
cstmt.setString(2, "Trupthi");
cstmt.setInt(3, 33);
cstmt.setInt(4, 4360);
cstmt.setString(5, "Ahmedabad");
cstmt.execute();
cstmt.setInt(1, 8);
cstmt.setString(2, "Mithili");
cstmt.setInt(3, 26);
cstmt.setInt(4, 4100);
cstmt.setString(5, "Vijayawada");
cstmt.execute();
cstmt.setInt(1, 9);
cstmt.setString(2, "Maneesh");
cstmt.setInt(3, 39);
cstmt.setInt(4, 4000);
cstmt.setString(5, "Hyderabad");
cstmt.execute();
cstmt.setInt(1, 10);
cstmt.setString(2, "Rajaneesh");
cstmt.setInt(3, 30);
cstmt.setInt(4, 6400);
cstmt.setString(5, "Delhi");
cstmt.execute();
cstmt.setInt(1, 11);
cstmt.setString(2, "Komal");
cstmt.setInt(3, 29);
cstmt.setInt(4, 8000);
cstmt.setString(5, "Ahmedabad");
cstmt.execute();
cstmt.setInt(1, 12);
cstmt.setString(2, "Manyata");
cstmt.setInt(3, 25);
cstmt.setInt(4, 5000);
cstmt.setString(5, "Vijayawada");
cstmt.execute();
System.out.println("Procedure called by passing required values......");
}
}输出
Connection established...... Procedure called by passing required values......
如果您使用 select 命令检索 customers 表的内容,您可以观察到新添加的记录如下所示。
mysql> select * from 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 | Ahmedabad | | 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 | +----+-----------+-----+---------+----------------+ 12 rows in set (0.00 sec)
数据结构
网络
关系数据库管理系统 (RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP