高級(jí)特性—存儲(chǔ)過程調(diào)用與結(jié)果集數(shù)據(jù)處理

2018-12-24 22:24 更新

針對(duì)于存儲(chǔ)過程,JDBC模塊提供了IProcedureOperator操作器接口及其默認(rèn)接口實(shí)現(xiàn)類DefaultProcedureOperator來幫助你完成,存儲(chǔ)過程有以下幾種調(diào)用方式,舉例說明:

  • 有輸入?yún)?shù)無輸出參數(shù):

    IConnectionHolder _conn = JDBC.get().getDefaultConnectionHolder();
    try {
        // 執(zhí)行名稱為`procedure_name`的存儲(chǔ)過程,并向該存儲(chǔ)過程轉(zhuǎn)入兩個(gè)字符串參數(shù)
        IProcedureOperator<Object[]> _opt = new DefaultProcedureOperator<Object[]>("procedure_name", _conn)
                .addParameter("param1")
                .addParameter("param2")
                .execute(IResultSetHandler.ARRAY);
        // 遍歷結(jié)果集集合
        for (List<Object[]> _item : _opt.getResultSets()) {
            ResultSetHelper.bind(_item).forEach(new ResultSetHelper.ItemHandler() {
                public boolean handle(ResultSetHelper.ItemWrapper wrapper, int row) throws Exception {
                    System.out.println(wrapper.toObject(new ArchiveVObject()).toJSON());
                    return true;
                }
            });
        }
    } finally {
        _conn.release();
    }
    
  • 有輸入輸出參數(shù):

    IConnectionHolder _conn = JDBC.get().getDefaultConnectionHolder();
    try {
        // 通過addOutParameter方法按存儲(chǔ)過程輸出參數(shù)順序指定JDBC參數(shù)類型
        new DefaultProcedureOperator("procedure_name", _conn)
                .addParameter("param1")
                .addParameter("param2")
                .addOutParameter(Types.VARCHAR)
                .execute(new IProcedureOperator.IOutResultProcessor() {
                    public void process(int idx, int paramType, Object result) throws Exception {
                        System.out.println(result);
                    }
                });
    } finally {
        _conn.release();
    }
    
  • 另一種寫法:

    JDBC.get().openSession(new ISessionExecutor<List<List<Object[]>>>() {
        public List<List<Object[]>> execute(ISession session) throws Exception {
            // 創(chuàng)建存儲(chǔ)過程操作器對(duì)象
            IProcedureOperator<Object[]> _opt = new DefaultProcedureOperator<Object[]>("procedure_name", session.getConnectionHolder())
                    .addParameter("param1")
                    .addParameter("param2")
                    .addOutParameter(Types.VARCHAR)
                    .addOutParameter(Types.INTEGER)
                    .setOutResultProcessor(new IProcedureOperator.IOutResultProcessor() {
                        public void process(int idx, int paramType, Object result) throws Exception {
                            System.out.println(result);
                        }
                    }).setResultSetHandler(IResultSetHandler.ARRAY);
            // 執(zhí)行
            _opt.execute();
            return _opt.getResultSets();
        }
    });
以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)