zoukankan      html  css  js  c++  java
  • 15、JDBC-CallableStatement

    一、存储过程

    创建

    CREATE DEFINER=CURRENT_USER PROCEDURE `adder`(IN a int, IN b int, OUT sum int)
    BEGIN
        DECLARE c int;
        
        if a is null
            then set a = 0;
        end if;
        
        if b is null
            then set b = 0;
        end if;
        
        set sum  = a + b;
    END

    代码方式

    import com.alibaba.druid.pool.DruidDataSource;
    import org.junit.jupiter.api.AfterEach;
    import org.junit.jupiter.api.BeforeEach;
    import org.junit.jupiter.api.Test;
    
    import java.sql.*;
    
    public class StoredProcedureTest {
    
        private Connection conn;
        private Statement statement;
        private CallableStatement callableStatement;
        private PreparedStatement preparedStatement;
        private ResultSet resultSet;
    
        @BeforeEach
        public void testDruid() throws SQLException {
            DruidDataSource dataSource = new DruidDataSource();
            dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
            dataSource.setUrl("jdbc:mysql://192.168.8.136:3306/jdbc");
            dataSource.setUsername("root");
            dataSource.setPassword("root");
    
            conn = dataSource.getConnection();
        }
    
        @AfterEach
        public void end() throws Exception {
            if (resultSet != null) {
                resultSet.close();
            }
            if (preparedStatement != null) {
                preparedStatement.close();
            }
            if (callableStatement != null) {
                callableStatement.close();
            }
            if (statement != null) {
                statement.close();
            }
            if (conn != null) {
                conn.close();
            }
        }
    
        @Test
        public void createProcedureAdder() throws SQLException {
            String queryDrop = "DROP PROCEDURE IF EXISTS adderP";
            String createProcedure = "CREATE DEFINER=`root`@`%` PROCEDURE `adderP`(IN a int, IN b int, OUT sum int)
    " +
                    "BEGIN
    " +
                    "	DECLARE c int;
    " +
                    "	if a is null then set a = 0;
    " +
                    "	end if;
    " +
                    "	if b is null then set b = 0;
    " +
                    "	end if;
    " +
                    "	set sum  = a + b;
    " +
                    "END";
            try {
                statement = conn.createStatement();
                statement.execute(queryDrop);
                statement.executeUpdate(createProcedure);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    调用

    @Test
    public void runStoredProcedures() {
        try {
            callableStatement = conn.prepareCall("{call adderP(?, ?, ?)}");
            // 输入
            callableStatement.setInt(1, 5);
            callableStatement.setInt(2, 5);
            // 输出
            callableStatement.registerOutParameter(3, Types.INTEGER);
            callableStatement.executeQuery();
    
            // 获取结果
            Integer result = callableStatement.getInt(3);
    
            System.out.println(result);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

     

    二、自定义函数

    创建

    CREATE DEFINER=`root`@`%` FUNCTION `adder`(`a` int,`b` int) RETURNS int(10)
    BEGIN
    --     变量定义需在函数开头
        DECLARE sum int UNSIGNED DEFAULT 0;
        
        if a is null then
            set a = 0;
        end if;
        
        if b is null then
            set b = 0;
        end if;
        
        set sum  = a + b;
        RETURN sum;
    END

    代码方式

    @Test
    public void createFunctionAdder() throws SQLException {
        String queryDrop = "DROP FUNCTION IF EXISTS adderF";
        String createProcedure = "CREATE DEFINER=CURRENT_USER FUNCTION `adderF`(`a` int,`b` int) RETURNS int(10)
    " +
                "BEGIN
    " +
                "-- 	变量定义需在函数开头
    " +
                "	DECLARE sum int UNSIGNED DEFAULT 0;
    " +
                "	if a is null then
    " +
                "		set a = 0;
    " +
                "	end if;
    " +
                "	if b is null then
    " +
                "		set b = 0;
    " +
                "	end if;
    " +
                "	set sum  = a + b;
    " +
                "	RETURN sum;
    " +
                "END";
        try {
            statement = conn.createStatement();
            statement.execute(queryDrop);
            statement.executeUpdate(createProcedure);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    调用

    @Test
    public void runFunctions() {
        try {
            preparedStatement = conn.prepareStatement("SELECT adderF(?, ?)");
            // 输入参数
            preparedStatement.setInt(1, 5);
            preparedStatement.setInt(2, 5);
            // 输出
            preparedStatement.executeQuery();
    
            // 获取结果
            resultSet = preparedStatement.getResultSet();
            if (resultSet.next()) {
                System.out.println(resultSet.getInt(1));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }


    官方文档

    https://www.cnblogs.com/lz2017/p/7500411.html

  • 相关阅读:
    [置顶] android ListView包含Checkbox滑动时状态改变
    Xamarin Android Gestures详解
    尝试在条件“$(_DeviceSdkVersion) >= 21”中对计算结果为“”而不是数字的“$(_DeviceSdkVersion)
    Xamarin Android自定义文本框
    C#四种深拷贝方法(转载)
    设置pictureBox的边框颜色(转载)
    C# 在运行中拖拽,改变控件大小位置类(转载)
    Ocelot + Consul的demo(二)集群部署
    Objective-C 简介
    计算机网络—概述
  • 原文地址:https://www.cnblogs.com/jhxxb/p/10469256.html
Copyright © 2011-2022 走看看