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;
    }
  • 相关阅读:
    形象理解ERP(转)
    禁用windows server 2008 域密码复杂性要求策略
    How to adding find,filter,remove filter on display method Form
    Windows Server 2008 R2激活工具
    How to using bat command running VS development SSRS report
    Creating Your First Mac AppGetting Started
    Creating Your First Mac AppAdding a Track Object 添加一个 Track 对象
    Creating Your First Mac AppImplementing Action Methods 实现动作方法
    Creating Your First Mac AppReviewing the Code 审查代码
    Creating Your First Mac AppConfiguring the window 设置窗口
  • 原文地址:https://www.cnblogs.com/zhangchunxi/p/2965670.html
Copyright © 2011-2022 走看看