zoukankan      html  css  js  c++  java
  • java后台调用存储过程总结

    在java中实现调用数据库中的存储过程小结:

    一、存储过程无返回参数的调用:

    public boolean callQuery(String[] args) throws Exception {        
        String pSql = "{call PK_INITIALIZATION.PRO_INIT_EVAInterface(?,?,?)}";
        Connection con = null;
        CallableStatement stCall = null; 
        con = this.getSession().connection();
        stCall = con.prepareCall(pSql); 
        for(int i=0;i<args.length;i++){
              stCall.setString(i+1, args[i]); 
        }
        stCall.registerOutParameter(3, Types.VARCHAR);
        stCall.execute();
        stCall.close();
        con.close(); 
        return true;
    }

    二、存储过程有返回参数的调用:(非列表)

    使用getString(参数所在的列)获取返回参数;

    public boolean callQuery(String[] args) throws Exception {        
        String pSql = "{call PK_INITIALIZATION.PRO_INIT_EVAInterface(?,?,?)}";
        Connection con = null;
        CallableStatement stCall = null; 
        con = this.getSession().connection();
        stCall = con.prepareCall(pSql); 
        for(int i=0;i<args.length;i++){
              stCall.setString(i+1, args[i]); 
        }
        stCall.registerOutParameter(3, Types.VARCHAR);
        stCall.execute();
        String y = stCall.getString(3);
        stCall.close();
        con.close(); 
        return true;
    }

    这里的stCall.getString(3),其中3指在存储过程中的返回参数位于第三列;

    三、返回参数为列表:

    由于oracle存储过程没有返回值,它的所有返回值都是通过out参数来替代的,列表同样也不例外,但由于是集合,所以不能用一般的参数,必须要用pagkage了.所以要分两部分,
    
    1,  建一个程序包。如下:
    
    CREATE OR REPLACE PACKAGE TESTPACKAGE  AS
    
     TYPE Test_CURSOR IS REF CURSOR;
    
    end TESTPACKAGE;
    
    2,建立存储过程,存储过程为:
    
    CREATE OR REPLACE PROCEDURE TESTC(p_CURSOR out TESTPACKAGE.Test_CURSOR) IS 
    
    BEGIN
    
        OPEN p_CURSOR FOR SELECT * FROM HYQ.TESTTB;
    
    END TESTC;

    java中调用存储过程:

    public ResultSet callQuery(String[] args) throws Exception {        
        String pSql = "{call TESTC(?)}";
        Connection con = null;
        CallableStatement stCall = null; 
    ResultSet rs = null; con
    = this.getSession().connection(); stCall = con.prepareCall(pSql); stCall.registerOutParameter(1, Types.VARCHAR); stCall.execute(); rs = (ResultSet)stCall.getObject(1);
    stCall.close(); con.close();
    return rs;
    }
  • 相关阅读:
    CF Mike and Feet (求连续区间内长度为i的最小值)单调栈
    BOOST_CLASS_EXPORT
    STL 源代码剖析 算法 stl_algo.h -- search
    烦人的Facebook分享授权
    [Swift]Scanner字符串扫描类
    [Swift]LeetCode682. 棒球比赛 | Baseball Game
    [Swift]LeetCode680. 验证回文字符串 Ⅱ | Valid Palindrome II
    [Swift]LeetCode679. 24点游戏 | 24 Game
    [Swift]LeetCode678. 有效的括号字符串 | Valid Parenthesis String
    [Swift]LeetCode677. 键值映射 | Map Sum Pairs
  • 原文地址:https://www.cnblogs.com/zhangchunxi/p/2965670.html
Copyright © 2011-2022 走看看