• 19【CallableStatement 接口】



    上一篇18【PreparedStatement接口详细解析】


    下一篇20【JDBC的事务处理】

    目录【MySQL零基础系列教程】



    19【CallableStatement 接口】

    1.1 CallableStatement 接口

    1.1.1 CallableStatement 简介

    CallableStatement是PreparedStatement的子类,主要是调用数据库中的存储过程/存储函数。并通过CallableStatement对象可以获取存储过程/存储函数的执行结果;

    1.1.2 CallableStatement的使用

    1)执行无参无返回
    • 测试数据准备:
    -- 创建一张测试表
    create table test(
    	id int primary key auto_increment,
    	name varchar(200)
    );
    
    -- 创建一个无参无返回的存储过程
    create procedure test1()
    begin
    	insert into test values(null,uuid());
    end;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 测试代码:
    package com.dfbz.demo;
    
    
    import com.dfbz.utils.JdbcUtils;
    import org.junit.Test;
    
    import java.sql.CallableStatement;
    import java.sql.Connection;
    import java.sql.SQLException;
    
    /**
     * @author lscl
     * @version 1.0
     * @intro: 执行存储过程
     */
    public class Demo14_CallableStatement {
    
        /**
         * 执行无参无返回
         * @throws SQLException
         */
        @Test
        public void test1() throws SQLException {
    
            // 获取数据库连接
            Connection connection = JdbcUtils.getConnection();
    
            // 定义存储过程
            CallableStatement cs = connection.prepareCall("call test1()");
    
            // 执行存储过程
            cs.execute();
            JdbcUtils.close(connection, cs);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    2)执行有参无返回
    • 定义存储过程:
    CREATE PROCEDURE test2 (in id int,in name varchar(30))
    begin
    	insert into test values(id,name);
    end ;
    
    • 1
    • 2
    • 3
    • 4
    • 测试代码:
    /**
     * 执行有参无返回
     * @throws SQLException
     */
    @Test
    public void test2() throws SQLException {
    
        // 获取数据库连接
        Connection connection = JdbcUtils.getConnection();
    
        // 定义存储过程
        CallableStatement cs = connection.prepareCall("call test2(?,?)");
        cs.setInt(1,10);
        cs.setString(2,"zhangsan");
    
        // 执行存储过程
        cs.execute();
        JdbcUtils.close(connection, cs);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    3)执行有参有返回
    • 定义存储过程:
    CREATE PROCEDURE test3 (in id int,out str1 varchar(30),out str2 varchar(30))
    begin
    	if id=1 then
    		set str1 = "Java";
    		set str2 = "薪资12k";
    	elseif id=2 then
    		set str1 = "UI";
    		set str2 = "薪资8k";
    	elseif id=3 then
    		set str1 = "产品";
    		set str2 = "薪资10k";
    	end if;
    	
    end;
    
    
    call test3(2,@str1,@str2);
    
    select @str1;
    select @str2;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 测试代码:
    /**
     * 执行有参有返回
     * @throws SQLException
     */
    @Test
    public void test3() throws SQLException {
    
        // 获取数据库连接
        Connection connection = JdbcUtils.getConnection();
    
        // 定义存储过程
        CallableStatement cs = connection.prepareCall("call test3(?,?,?)");
    
        // 输入参数
        cs.setInt(1,2);
    
        // 输出参数
        cs.registerOutParameter(2, Types.VARBINARY);
        cs.registerOutParameter(3, Types.VARBINARY);
    
        // 执行存储过程
        cs.execute();
    
        // 根据参数名获取值
        String str1 = cs.getString("str1");
        String str2 = cs.getString("str2");
    
        System.out.println(str1);
        System.out.println(str2);
        System.out.println("-----------------");
    
        // 根据参数索引获取
        str1 = cs.getString(2);
        str2 = cs.getString(3);
    
        System.out.println(str1);
        System.out.println(str2);
    
        System.out.println(str1);
        System.out.println(str2);
    
        JdbcUtils.close(connection, cs);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    4)执行存储函数

    存储函数必须有返回值,JDBC执行存储函数就相当于执行存储过程时多了一个输出参数而已;

    • 定义存储函数:
    -- 定义存储函数
    create function test4(num int)
    returns int			
    begin
    	return num;
    end;
    
    -- 执行存储函数
    select test4(10);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 测试代码:
    /**
     * 执行存储函数
     *
     * @throws SQLException
     */
    @Test
    public void test4() throws SQLException {
    
        // 获取数据库连接
        Connection connection = JdbcUtils.getConnection();
    
        // 定义存储过程
        CallableStatement cs = connection.prepareCall("{?=call test4(?)}");
    
        // 注册输出参数(存储函数的结果集)
        cs.registerOutParameter(1,Types.INTEGER);
    
        // 输入参数
        cs.setInt(2, 100);
    
        // 执行存储函数
        cs.execute();
    
        // 获取第一个占位符的值
        int result = cs.getInt(1);
        
        System.out.println(result);
    
        JdbcUtils.close(connection, cs);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    5)其他情况

    有的时候执行存储过程时,其内部会产生结果集,该结果集并没有当做参数返回出来,JDBC也支持获取这部分的结果集;

    • 定义存储过程:
    CREATE PROCEDURE test5 (out str varchar(30))
    begin
    	select * from test;
    	
    	set str = "success";
    end;
    
    call test5(@str)
    
    select @str;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    在这里插入图片描述

    • 测试代码:
    /**
     * 执行存储函数
     *
     * @throws SQLException
     */
    @Test
    public void test5() throws SQLException {
    
        // 获取数据库连接
        Connection connection = JdbcUtils.getConnection();
    
        // 定义存储过程
        CallableStatement cs = connection.prepareCall("call test5(?)");
    
        // 注册输出参数(存储函数的结果集)
        cs.registerOutParameter(1, Types.VARBINARY);
    
        // 执行存储函数,获取存储过程内部产生的结果集
        ResultSet rs= cs.executeQuery();
    
        while (rs.next()){
    
            int id = rs.getInt("id");
            String name = rs.getString("name");
    
            System.out.println("id: "+id);
            System.out.println("name: "+name);
    
            System.out.println("----------------");
        }
    
        // 获取存储过程的输出参数
        String result = cs.getString("str");
        System.out.println(result);
    
        JdbcUtils.close(connection, cs);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
  • 相关阅读:
    软考系统架构设计师考试冲刺攻略
    Liunx两台服务器实现相互SSH免密登录
    Flutter框架性泛学习系列之二、Flutter应用层(Application Layer)上-常用Widgets与简单动画
    php健身房教练预约系统网站
    本地化部署对比选型推荐——百数低代码平台
    Java 枚举类型与泛型-第13章
    命令行客户端-连接服务端&操作数据库
    C++入门基础知识(2)
    简单的 JSONParser
    408-2016真题
  • 原文地址:https://blog.csdn.net/Bb15070047748/article/details/126570057