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.gifpublicStringcallProc(StringprocName,String[][]params)...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.giflog.debug("procName:"+procName);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.giffor(inti=0;params!=null&&i<params.length;i++)...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifif(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.gifparams[1]="%";
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif}
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.giflog.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.giftry...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifCallableStatementproc=null;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifConnectionconn=null;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifconn=this.getSession().connection();
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifintindex=procName.indexOf("?");
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifprocName=procName.substring(0,index)+"?,"
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif+procName.substring(index);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifproc=conn.prepareCall(procName);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifintparamsNum=params.length;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.giffor(intk=0;k<paramsNum;k++)...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gifif(params[0].equalsIgnoreCase("String"))...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifproc.setString(k+1,params[1]);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif}elseif(params[0].equalsIgnoreCase("Long"))...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifproc.setLong(k+1,Long.parseLong(params[1]));
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif}elseif(params[0].equalsIgnoreCase("Integer"))...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifproc.setInt(k+1,Integer.parseInt(params[1]));
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif}elseif(params[0].equalsIgnoreCase("BigDecimal"))...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifproc.setBigDecimal(k+1,newBigDecimal(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.gifproc.registerOutParameter(paramsNum+1,Types.VARCHAR);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifproc.execute();
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifreturnproc.getString(paramsNum+1);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif}catch(Exceptione)...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifthrownewRuntimeException(e);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif}
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif}
页:
[1]