直接上代码:
/** * 执行返回一个输出参数,若干输入参数的存储过程 * 方法第一个参数为存储过程名称,第二个参数为存放输入参数的数组(按顺序存放),方法返回值为存储过程输出参数 */ public String execProcedureWithOutParam(final String procName,final String[] inParams){ String returnVal=(String)getJdbcTemplate().execute(new CallableStatementCreator() { public CallableStatement createCallableStatement(Connection con) throws SQLException { int procArgNum=inParams.length+1; String storedProc="{call "+procName+"("; for (int i = 0; i < procArgNum; i++) { if(i!=procArgNum-1){ storedProc+="?,"; }else { storedProc+="?"; } } storedProc+=")}"; CallableStatement cs=con.prepareCall(storedProc); for (int i = 1; i <=inParams.length; i++) { cs.setString(i, inParams[i-1]); } cs.registerOutParameter(inParams.length+1, java.sql.Types.VARCHAR); return cs; } },new CallableStatementCallback() { public Object doInCallableStatement(CallableStatement cs) throws SQLException, DataAccessException { cs.execute(); return cs.getString(inParams.length+1); } }); return returnVal; }
时间: 2024-10-19 03:47:16