zoukankan      html  css  js  c++  java
  • 事务--存储过程

    创建事务
    
    delimiter //
    
    CREATE PROCEDURE sp_count(OUT param INT)
    BEGIN 
        SELECT COUNT(*) INTO param FROM persons;
    END
    //
    package test;
    
    import java.sql.CallableStatement;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.SQLException;
    import java.sql.Types;
    
    import org.junit.Before;
    import org.junit.jupiter.api.Test;
    
    
    /**
    *@author :王团结
    *@version: 2019年6月20日上午10:48:28
    *类说明:
    */
    public class TestCallableStatement {
        private Connection conn;
        /**
         * 
         */
        @Before
        public void iniConn() {
            // 注册驱动程序、连接、时区一定要加上
            String url = "jdbc:mysql://localhost:3306/mybase?serverTimezone=UTC";
    
            String username = "root";
            String password = "18339401841";
    
            try {
                // 获得连接
                conn = DriverManager.getConnection(url, username, password);
    
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        
        /**
         * select
         * @throws Exception 
         */
        @Test
        public void test1() throws Exception {
            iniConn();
            //输出
            String sql="{call sp_count(?)}";
            //创建
            CallableStatement cst=conn.prepareCall(sql);
            //注册输出参数
            cst.registerOutParameter(1, Types.INTEGER);
            //执行存储过程
            cst.execute();
            //取得输出的参数
            int id=cst.getInt(1);
            System.out.println(id);
        }
        /**
         * select
         * @throws Exception 
         */
        @Test
        public void sp_add() throws Exception {
            iniConn();
            //输出
            String sql="{call sp_add(?,?,?)}";
            //创建
            CallableStatement cst=conn.prepareCall(sql);
            
            //对于输入 参数需要绑定参数值
            cst.setInt(1, 2);
            cst.setInt(2, 2);
            //注册输出参数
            cst.registerOutParameter(3, Types.INTEGER);
            //执行存储过程
            cst.execute();
            //取得输出的参数
            int id=cst.getInt(3);
            System.out.println(id);
        }
        /**
         * select
         * @throws Exception 
         */
        @Test
        public void sp_subtract() throws Exception {
            iniConn();
            //输出
            String sql="{call sp_subtract(?,?)}";
            //创建
            CallableStatement cst=conn.prepareCall(sql);
            
            //对于输入 参数需要绑定参数值
            cst.setInt(1, 2);
            cst.setInt(2, 2);
            //注册输出参数
            cst.registerOutParameter(2, Types.INTEGER);
            //执行存储过程
            cst.execute();
            //取得输出的参数
            int id=cst.getInt(2);
            System.out.println(id);
        }
        /**
         * select
         * @throws Exception 
         */
        @Test
        public void sp_biginsert() throws Exception {
            iniConn();
            //输出
            String sql="{call sp_biginsert(?)}";
            //创建
            CallableStatement cst=conn.prepareCall(sql);
            
            //对于输入 参数需要绑定参数值
            cst.setInt(1, 100000);
            //执行存储过程
            cst.execute();
            //取得输出的参数
        }
        /**
         * 调用函数
         * SET GLOBAL log_bin_trust_function_creators = 1; 
         *CREATE FUNCTION f_sum(a INT,b int) RETURNS INT
         *RETURN a+b;
         * @throws Exception 
         */
        @Test
        public void f_sum() throws Exception {
            iniConn();
            //输出
            String sql="{?=call f_sum(?,?)}";
            //创建
            CallableStatement cst=conn.prepareCall(sql);
            cst.setInt(2, 100);
            cst.setInt(3, 200);
            cst.registerOutParameter(1, Types.INTEGER);
            
            //执行存储过程
            cst.execute();
            int res=cst.getInt(1);
            //取得输出的参数
            System.out.println(res);
        }
    }
  • 相关阅读:
    find-the-distance-from-a-3d-point-to-a-line-segment
    Distance Point to Line Segment
    Shortest distance between a point and a line segment
    Splitting and Merging--区域分裂与合并算法
    手写区域分裂合并算法
    free online editor
    SQL server ide
    online c++ compiler
    online sql editor
    Web-based SQL editor
  • 原文地址:https://www.cnblogs.com/King-boy/p/11058686.html
Copyright © 2011-2022 走看看