登录
首页 >  数据库 >  MySQL

如何在 JDBC 解释中使用可调用语句调用存储过程?

来源:tutorialspoint

时间:2023-08-27 20:09:58 334浏览 收藏

在IT行业这个发展更新速度很快的行业,只有不停止的学习,才不会被行业所淘汰。如果你是数据库学习者,那么本文《如何在 JDBC 解释中使用可调用语句调用存储过程?》就很适合你!本篇内容主要包括##content_title##,希望对大家的知识积累有所帮助,助力实战开发!

您可以使用 CallableStatement 接口调用 SQL 存储过程。 Callable 语句可以具有输入参数、输出参数或两者。

您可以使用 prepareCall() 创建 CallableStatement(接口)的对象> Connection 接口的方法。此方法接受一个表示调用存储过程的查询的字符串变量,并返回一个 CallableStatement 对象。

假设您有一个过程名称 myProcedure 在数据库,您可以准备一个可调用语句:

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

然后,您可以使用 CallableStatement 接口的 setter 方法为占位符设置值,并使用 execute() 方法执行可调用语句,如下所示。

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

如果该过程没有输入值,您只需准备可调用语句并执行它,如下所示:

CallableStatement cstmt = con.prepareCall("{call myProcedure()}");
cstmt.execute();

示例

假设 MySQL 数据库中有一个名为 Dispatches 的表,其中包含以下数据:

+--------------+------------------+------------------+----------------+
| Product_Name | Date_Of_Dispatch | Time_Of_Dispatch | Location       |
+--------------+------------------+------------------+----------------+
| KeyBoard     | 1970-01-19       | 08:51:36         | Hyderabad      |
| Earphones    | 1970-01-19       | 05:54:28         | Vishakhapatnam |
| Mouse        | 1970-01-19       | 04:26:38         | Vijayawada     |
+--------------+------------------+------------------+----------------+

如果我们创建了一个名为 myProcedure 的过程来从此表中检索值,如下所示:

Create procedure myProcedure ()
-> BEGIN
-> SELECT * FROM Dispatches;
-> END //

示例

下面是一个使用 JDBC 程序调用上述存储过程的 JDBC 示例。

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
public class CallingProcedure {
   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/sampleDB";
      Connection con = DriverManager.getConnection(mysqlUrl, "root", "password");
      System.out.println("Connection established......");

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

      //Retrieving the result
      ResultSet rs = cstmt.executeQuery();
      while(rs.next()) {
         System.out.println("Product Name: "+rs.getString("Product_Name"));
         System.out.println("Date of Dispatch: "+rs.getDate("Date_Of_Dispatch"));
         System.out.println("Time of Dispatch: "+rs.getTime("Time_Of_Dispatch"));
         System.out.println("Location: "+rs.getString("Location"));
         System.out.println();
      }
   }
}

输出

Connection established......
Product Name: KeyBoard
Date of Dispatch: 1970-01-19
Time of Dispatch: 08:51:36
Location: Hyderabad

Product Name: Earphones
Date of Dispatch: 1970-01-19
Time of Dispatch: 05:54:28
Location: Vishakhapatnam

Product Name: Mouse
Date of Dispatch: 1970-01-19
Time of Dispatch: 04:26:38
Location: Vijayawada

理论要掌握,实操不能落!以上关于《如何在 JDBC 解释中使用可调用语句调用存储过程?》的详细介绍,大家都掌握了吧!如果想要继续提升自己的能力,那么就来关注golang学习网公众号吧!

声明:本文转载于:tutorialspoint 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>