W3Cschool
恭喜您成為首批注冊用戶
獲得88經(jīng)驗值獎勵
針對于存儲過程,JDBC模塊提供了IProcedureOperator
操作器接口及其默認接口實現(xiàn)類DefaultProcedureOperator
來幫助你完成,存儲過程有以下幾種調用方式,舉例說明:
有輸入?yún)?shù)無輸出參數(shù):
IConnectionHolder _conn = JDBC.get().getDefaultConnectionHolder(); try { // 執(zhí)行名稱為`procedure_name`的存儲過程,并向該存儲過程轉入兩個字符串參數(shù) IProcedureOperator<Object[]> _opt = new DefaultProcedureOperator<Object[]>("procedure_name", _conn) .addParameter("param1") .addParameter("param2") .execute(IResultSetHandler.ARRAY); // 遍歷結果集集合 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方法按存儲過程輸出參數(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)建存儲過程操作器對象 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(); } });
Copyright©2021 w3cschool編程獅|閩ICP備15016281號-3|閩公網(wǎng)安備35020302033924號
違法和不良信息舉報電話:173-0602-2364|舉報郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號
聯(lián)系方式:
更多建議: