首頁  >  文章  >  資料庫  >  JDBC 中的 CallableStatement 是什麼?

JDBC 中的 CallableStatement 是什麼?

PHPz
PHPz轉載
2023-09-07 23:53:02897瀏覽

JDBC 中的 CallableStatement 是什么?

CallableStatement 介面提供執行預存程序的方法。由於 JDBC API 提供了預存程序 SQL 轉義語法,因此您可以以單一標準方式呼叫所有 RDBMS 的預存程序。

建立 CallableStatement

您可以建立 Connection介面的prepareCall()方法來呼叫strong>CallableStatement#(介面)。此方法接受一個表示呼叫預存程序的查詢的字串變量,並傳回一個 CallableStatement 物件。

Callable 語句可以有輸入參數、輸出參數或兩者都有。若要將輸入參數傳遞給過程調用,您可以使用佔位符並使用 CallableStatement 介面提供的 setter 方法(setInt()、setString()、setFloat())為這些參數設定值。

假設您資料庫中有一個名為myProcedure 的過程,您可以準備一個可調用語句,如下所示:

//Preparing a CallableStatement
CallableStatement cstmt = con.prepareCall("{call myProcedure(?, ?, ?)}");

為輸入參數設定值

您可以使用setter 方法為過程呼叫的輸入參數設定值。

它們接受兩個參數,其中一個是整數值表示輸入參數的放置索引,另一個是 int 或、String 或、float 等…表示您需要傳遞的值 向過程輸入參數。

注意:除了索引之外,您還可以以字串格式傳遞參數名稱。

cstmt.setString(1, "Raghav");
cstmt.setInt(2, 3000);
cstmt.setString(3, "Hyderabad");

執行 Callable 語句

建立 CallableStatement 物件後,您可以使用 execute() 方法之一來執行它。

cstmt.execute();

範例

假設MySQL 資料庫中有一個名為Employee 的表,其中包含以下資料:

+---------+--------+----------------+
| Name    | Salary | Location       |
+---------+--------+----------------+
| Amit    | 30000  | Hyderabad      |
| Kalyan  | 40000  | Vishakhapatnam |
| Renuka  | 50000  | Delhi          |
| Archana | 15000  | Mumbai         |
+---------+--------+----------------+

我們建立了一個名為myProcedure 的過程會將值插入到該表中,如下所示:

Create procedure myProcedure (IN name VARCHAR(30), IN sal INT, IN loc VARCHAR(45))
   -> BEGIN
   -> INSERT INTO Employee(Name, Salary, Location) VALUES (name, sal, loc);
   -> END //

下面是一個JDBC 範例,它使用可呼叫語句來呼叫上面建立的過程,將新記錄插入到Employee 表中。

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class CallableStatementExample {
   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://localhost/testdb";
      Connection con = DriverManager.getConnection(mysqlUrl, "root", "password");
      System.out.println("Connection established......");
      //Preparing a CallableStateement
      CallableStatement cstmt = con.prepareCall("{call myProcedure(?, ?, ?)}");

      cstmt.setString(1, "Raghav");
      cstmt.setInt(2, 3000);
      cstmt.setString(3, "Hyderabad");

      cstmt.setString(1, "Kalyan");
      cstmt.setInt(2, 4000);
      cstmt.setString(3, "Vishakhapatnam");

      cstmt.setString(1, "Rukmini");
      cstmt.setInt(2, 5000);
      cstmt.setString(3, "Delhi");

      cstmt.setString(1, "Archana");
      cstmt.setInt(2, 15000);
      cstmt.setString(3, "Mumbai");

      cstmt.execute();
      System.out.println("Rows inserted ....");
   }
}

輸出

Connection established......
Rows inserted ....

如果使用 select 查詢檢索 Employee 表的內容,您可以觀察到新新增的記錄,如下所示:

mysql> select * from employee;
+---------+--------+----------------+
| Name    | Salary | Location       |
+---------+--------+----------------+
| Amit    | 30000  | Hyderabad      |
| Kalyan  | 40000  | Vishakhapatnam |
| Renuka  | 50000  | Delhi          |
| Archana | 15000  | Mumbai         |
| Raghav  | 3000   | Hyderabad      |
| Raghav  | 3000   | Hyderabad      |
| Kalyan  | 4000   | Vishakhapatnam |
| Rukmini | 5000   | Delhi          |
| Archana | 15000  | Mumbai         |
+---------+--------+----------------+
9 rows in set (0.00 sec)

以上是JDBC 中的 CallableStatement 是什麼?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:tutorialspoint.com。如有侵權,請聯絡admin@php.cn刪除