• 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;
    }
  • 相关阅读:
    [恢]hdu 2391
    [恢]hdu 2352
    [恢]hdu 2393
    [恢]hdu 1868
    [恢]hdu 1279
    [恢]hdu 2086
    [恢]hdu 1405
    [恢]hdu 2088
    [恢]hdu 2106
    [恢]hdu 2537
  • 原文地址:https://www.cnblogs.com/zhangchunxi/p/2965670.html
Copyright © 2020-2023  润新知