zoukankan      html  css  js  c++  java
  • Hibernate调用存储过程和函数

     操作大批量数据或复杂逻辑,考虑到执行效率和代码量的时候,存储过程和函数在数据库中是预编译好的,调用执行效率高

    // 调用过程 {call 过程名称(?,?,?)}

    public static void test1() throws Exception {
    Connection connection  = request.getSession().connection();
    String sql = "{call addmethod(?,?,?)}";
    CallableStatement call = connection.prepareCall(sql);
    // 设置输入参数
    call.setInt(1, 3);
    call.setInt(2, 7);
    // 设置输出参数
    call.registerOutParameter(3, Types.INTEGER);

    call.execute();
    int ans = call.getInt(3);
    System.out.println(ans);

    }

    // 调用函数{?= call 函数名称(?,?)}

    public static void test3() throws Exception {
    Connection connection = request.getSession().getConnection();
    String sql = "{?= call addmethod1(?,?)}";
    CallableStatement call = connection.prepareCall(sql);
    // 设置输入参数
    call.setInt(2, 3);
    call.setInt(3, 7);
    // 设置返回值
    call.registerOutParameter(1, Types.INTEGER);

    call.execute();
    int ans = call.getInt(1);
    System.out.println(ans);

    }

    oracle存储过程

    create or replace procedure addmethod(n1 in number,n2 in number,ans out number) as
    begin
    select n1 + n2 into ans from dual;
    end;

    oracle函数

    create or replace function addmethod1(n1 number,n2 number,ans out number) 

    Result number;

    begin

    select n1 + n2 into ans from dual;

    return ans;

    end;

  • 相关阅读:
    yum install nginx
    逻辑分区增加空间 vm中
    pbspro build rpm and installation
    centos 6 and 7 to modify hostname
    activeMQ
    cgo在mac上编译
    redis学习
    Spring Boot 2+gRPC 学习系列1:搭建Spring Boot 2+gRPC本地项目
    KumuluzEE
    前端实现“查看更多”效果
  • 原文地址:https://www.cnblogs.com/laotan/p/3621810.html
Copyright © 2011-2022 走看看