importjava.sql.Types;importjavax.sql.DataSource;importorg.springframework.jdbc.core.SqlParameter;importorg.springframework.jdbc.object.SqlUpdate;publicclassUpdateCreditRatingextendsSqlUpdate {publicUpdateCreditRating(DataSource ds) {setDataSource(ds);setSql("update customer set credit_rating = ? where id = ?");declareParameter(new SqlParameter("creditRating",Types.NUMERIC));declareParameter(new SqlParameter("id",Types.NUMERIC));compile(); } /** * @param id for the Customer to be updated * @param rating the new value for credit rating * @return number of rows updated */publicintexecute(int id,int rating) {returnupdate(rating, id); }}
importjava.sql.Types;importjava.util.Date;importjava.util.HashMap;importjava.util.Map;importjavax.sql.DataSource;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.jdbc.core.SqlOutParameter;importorg.springframework.jdbc.object.StoredProcedure;publicclassStoredProcedureDao {privateGetSysdateProcedure getSysdate; @Autowiredpublicvoidinit(DataSource dataSource) {this.getSysdate=newGetSysdateProcedure(dataSource); }publicDategetSysdate() {returngetSysdate.execute(); }privateclassGetSysdateProcedureextendsStoredProcedure {privatestaticfinalString SQL ="sysdate";publicGetSysdateProcedure(DataSource dataSource) {setDataSource(dataSource);setFunction(true);setSql(SQL);declareParameter(new SqlOutParameter("date",Types.DATE));compile(); }publicDateexecute() {// the 'sysdate' sproc has no input parameters, so an empty Map is supplied...Map<String,Object> results =execute(newHashMap<String,Object>());Date sysdate = (Date) results.get("date");return sysdate; } }}
下面的StoredProcedure示例包含两个输出参数(在本例中为Oracle REF游标):
importjava.util.HashMap;importjava.util.Map;importjavax.sql.DataSource;importoracle.jdbc.OracleTypes;importorg.springframework.jdbc.core.SqlOutParameter;importorg.springframework.jdbc.object.StoredProcedure;publicclassTitlesAndGenresStoredProcedureextendsStoredProcedure {privatestaticfinalString SPROC_NAME ="AllTitlesAndGenres";publicTitlesAndGenresStoredProcedure(DataSource dataSource) { super(dataSource, SPROC_NAME);declareParameter(new SqlOutParameter("titles",OracleTypes.CURSOR,new TitleMapper()));declareParameter(new SqlOutParameter("genres",OracleTypes.CURSOR,new GenreMapper()));compile(); }publicMap<String,Object> execute() {// again, this sproc has no input parameters, so an empty Map is suppliedreturn super.execute(newHashMap<String,Object>()); }}