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