如何使用JDBC API在数据库中创建函数?

像过程一样,您也可以在数据库中创建函数并将其存储。

语法

以下是在(MySQL)数据库中创建函数的语法:

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

要使用JDBC API在数据库中创建函数,您需要:

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

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

  • Create Statement:使用Connection接口的createStatement()方法创建一个Statement对象。

  • 执行查询:使用execute()Statement接口的方法执行查询

假设我们有一个名为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类型的参数,并返回代表指定雇员的出生日期的date对象。

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 {
      //注册驱动程序
      DriverManager.registerDriver(new com.mysql.jdbc.Driver());
      //获得连接
      String mysqlUrl = "jdbc:mysql://localhost/MyDatabase";
      Connection con = DriverManager.getConnection(mysqlUrl, "root", "password");
      System.out.println("Connection established......");
      //创建语句
      Statement stmt = con.createStatement();
      //查询创建函数
      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";
      //执行查询
      stmt.execute(query);
      System.out.println("Function Created......");
   }
}

输出结果

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

SHOW CREATE FUNCTION函数名命令显示指定函数的源代码,如果它不存在,你会得到一个错误。

使用以下命令,验证是否在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)