W3Cschool
恭喜您成為首批注冊(cè)用戶
獲得88經(jīng)驗(yàn)值獎(jiǎng)勵(lì)
針對(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(); } });
Copyright©2021 w3cschool編程獅|閩ICP備15016281號(hào)-3|閩公網(wǎng)安備35020302033924號(hào)
違法和不良信息舉報(bào)電話:173-0602-2364|舉報(bào)郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號(hào)
聯(lián)系方式:
更多建議: