日期:2014-05-16 浏览次数:20506 次
IF OBJECT_ID('dbo.sp_xx') IS NOT NULL BEGIN DROP PROCEDURE dbo.sp_xx IF OBJECT_ID('dbo.sp_xx') IS NOT NULL PRINT '<<< FAILED DROPPING PROCEDURE dbo.sp_xx >>>' ELSE PRINT '<<< DROPPED PROCEDURE dbo.sp_xx >>>' END go create proc sp_xx ( @userid int) as begin select personid, personname from person where personid = @userid select teamid, teamname from team end EXEC sp_procxmode 'dbo.sp_xx','unchained' go
private class ProcWithResultSet extends StoredProcedure { public ProcWithResultSet(DataSource dataSource) { setDataSource(dataSource); setSql("sp_xx"); //declareParameter(new SqlParameter("userid", java.sql.Types.INTEGER)); //(1) //declare the first out param in the sp, the name rsPerson is the key of result map declareParameter(new SqlReturnResultSet("rsPerson", new PersonRowMapper())); //declare the second out param in the sp, the name rsTeam is the key of result map declareParameter(new SqlReturnResultSet("rsTeam", new TeamRowMapper())); //declare input params, the name userid is the IN params of the param declareParameter(new SqlParameter("userid", java.sql.Types.INTEGER)); //(2) compile(); } public Map getRsFromSP(int userID) { Map map = new HashMap(); map.put("userid", new Integer(userID)); return execute(map); } } private class PersonRowMapper implements RowMapper { public Object mapRow(ResultSet rs, int rowNum) throws SQLException { UserInfo u = new UserInfo(); u.setPersonID(rs.getInt(1)); u.setUserName(rs.getString(2)); return u; } } private class TeamRowMapper implements RowMapper { public Object mapRow(ResultSet rs, int rowNum) throws SQLException { return new PropertyValue(rs.getInt(1)+"", rs.getString(2)); } }
/** * Extract returned ResultSets from the completed stored procedure. * @param cs JDBC wrapper for the stored procedure * @param parameters Parameter list for the stored procedure * @return Map that contains returned results */ protected Map extractReturnedResultSets(CallableStatement cs, List parameters, int updateCount) throws SQLException { Map returnedResults = new HashMap(); int rsIndex = 0; boolean moreResults; do { if (updateCount == -1) { //(3) Object param = null; if (parameters != null && parameters.size() > rsIndex) { param = parameters.get(rsIndex); //(4) } if (param instanceof SqlReturnResultSet) { SqlReturnResultSet rsParam = (SqlReturnResultSet) param; returnedResults.putAll(processResultSet(cs.getResultSet(), rsParam)); //(5) } else { logger.warn("Results returned from stored procedure but a corresponding " + "SqlOutParameter/SqlReturnResultSet parameter was not declared"); } rsIndex++; } moreResults = cs.getMoreResults(); //(6) updateCount = cs.getUpdateCount(); if (logger.isDebugEnabled()) { logger.debug("CallableStatement.getUpdateCount() returned " + updateCount); } } while (moreResults || updateCount != -1); return returnedResults; }