benben 发表于 2013-1-13 18:58:24

java调用存储过程

 
有两种方式:
1、通过输出参数返回一个字符串(可以由多个输出参数,这里我们只演示一个的情况)
DB2过程:(只是示例,不要直接去运行,我没有时间去调试存储过程)
CREATE PROCEDURE test1
 (IN  V_IARG1 VARCHAR(40),
  IN V_IARG2 VARCHAR(20),
  IN V_IARG3 VARCHAR(20),
  OUT  V_ORET VARCHAR(40)
 )
  SPECIFIC test1
  LANGUAGE SQL
P1:BEGIN
    SET v_oret = '1';
    RETURN 0;
END P1
oracle类似上面,不再写了。
java方法:
<div style="padding-right: 5.4pt; padding-left: 5.4pt; background: #e6e6e6; padding-bottom: 4px; width: 95%; padding-top: 4px;">http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif    public String callProc(String procName, String[][] params) ...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif        log.debug("procName:" + procName);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif        for (int i = 0; params != null && i < params.length; i++) ...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif            if (params[1] == null || params[1].equals("")
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif                    || params[1].equals("null")) ...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif                params[1] = "%";
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif            }
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif            log.debug(params[1]);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif        }
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif        try ...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif            CallableStatement proc = null;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif            Connection conn = null;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif            conn = this.getSession().connection();
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif            int index = procName.indexOf("?");
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif            procName = procName.substring(0, index) + "?,"
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif                    + procName.substring(index);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif            proc = conn.prepareCall(procName);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif            int paramsNum = params.length;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif            for (int k = 0; k < paramsNum; k++) ...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif                if (params[0].equalsIgnoreCase("String")) ...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif                    proc.setString(k + 1, params[1]);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif                } else if (params[0].equalsIgnoreCase("Long")) ...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif                    proc.setLong(k + 1, Long.parseLong(params[1]));
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif                } else if (params[0].equalsIgnoreCase("Integer")) ...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif                    proc.setInt(k + 1, Integer.parseInt(params[1]));
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif                } else if (params[0].equalsIgnoreCase("BigDecimal")) ...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif                    proc.setBigDecimal(k + 1, new BigDecimal(params[1]));
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif                }
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif            }
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif            proc.registerOutParameter(paramsNum + 1, Types.VARCHAR);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif            proc.execute();
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif            return proc.getString(paramsNum + 1);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif        } catch (Exception e) ...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif            throw new RuntimeException(e);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif        }
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif    }
页: [1]
查看完整版本: java调用存储过程