如何使用 JDBC API 在資料庫中建立函式?


與儲存過程類似,您也可以在資料庫中建立函式並存儲它們。

語法

以下是建立 (MySQL) 資料庫中函式的語法

CREATE FUNCTION Function_Name(input_arguments) RETURNS output_parameter
BEGIN
   declare variables;
   statements . . . . . . . . . .
   return data_type;
END

要使用 JDBC API 在資料庫中建立函式,您需要

  • **註冊驅動程式**: 使用 **DriverManager** 類的 **registerDriver()** 方法註冊類。將驅動程式類名作為引數傳遞給它。

  • **建立連線**: 使用 **DriverManager** 類的 **getConnection()** 方法連線到資料庫。將 URL(字串)、使用者名稱(字串)、密碼(字串)作為引數傳遞給它。

  • **建立語句**: 使用 **Connection** 介面的 **createStatement()** 方法建立一個 Statement 物件。

  • **執行查詢**: 使用 Statement 介面的 execute() 方法執行查詢

假設我們有一個名為 EmployeeDetails 的表,其描述如下

+----------+--------------+------+-----+---------+-------+
| Field    | Type         | Null | Key | Default | Extra |
+----------+--------------+------+-----+---------+-------+
| Name     | varchar(255) | YES  |     | NULL    |       |
| DOB      | date         | YES  |     | NULL    |       |
| Location | varchar(255) | YES  |     | NULL    |       |
+----------+--------------+------+-----+---------+-------+

以下 JDBC 程式建立與 MySQL 資料庫的連線,並建立一個名為 getDob() 的函式。

此函式接受一個 VARCHAR 型別的引數,表示員工的姓名,並返回一個表示指定員工出生日期的日期物件。

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class CreatingFunctionsExample {
   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......");
      //Creating the Statement
      Statement stmt = con.createStatement();
      //Query to create a function
      String query = "CREATE FUNCTION getDob(emp_name VARCHAR(50)) RETURNS DATE " +
         "BEGIN " +
         " declare dateOfBirth DATE;" +
         " select DOB into dateOfBirth from employeedetails where Name = emp_name;" +
         " return dateOfBirth;" +
         "END";
      //Executing the query
      stmt.execute(query);
      System.out.println("Function Created......");
   }
}

輸出

Connection established......
Function Created......

如果存在,則 *SHOW CREATE FUNCTION* **function_name** 命令顯示指定函式的原始碼,如果不存在,則會收到錯誤。

使用此命令驗證 MySQL 資料庫中是否建立了名為 getDob 的函式,如下所示

mysql> SHOW CREATE FUNCTION getDob;
+----------+----------------------------------------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+----------------------+----------------------+--------------------+
| Function | sql_mode | Create Function | character_set_client | collation_connection | Database Collation |
+----------+----------------------------------------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+----------------------+----------------------+--------------------+
| getDob | STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION | CREATE DEFINER=`root`@`localhost` FUNCTION `getDob`(emp_name VARCHAR(50)) RETURNS date
BEGIN declare dateOfBirth DATE; select DOB into dateOfBirth from employeedetails where Name = emp_name; return dateOfBirth; END | utf8 | utf8_general_ci | utf8_general_ci |
+----------+----------------------------------------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+----------------------+----------------------+--------------------+
1 row in set (0.00 sec)

更新於: 2019-07-30

1K+ 瀏覽量

開啟您的 職業生涯

透過完成課程獲得認證

開始學習
廣告

© . All rights reserved.