Embracing the Messiness in Search of Epic Solutions

Spring: Invoking Stored Procedure




There are many ways to skin a cat… so is invoking a stored procedure using Spring.

Let’s assume we want to invoke the following stored procedure that accepts 3 arguments (person ID, start date, end date):-

{ call calendar_sp (?, ?, ?) }

This stored procedure returns a result set where each row contains a person ID, a date and a description.


Create a class that extends org.springframework.jdbc.object.StoredProcedure:-

public class CalendarStoredProcedure extends StoredProcedure {</code>

    public CalendarStoredProcedure(DataSource dataSource) {
        super(dataSource, "calendar_sp");

        // input parameters
        declareParameter(new SqlParameter("person_id", Types.INTEGER));
        declareParameter(new SqlParameter("start_date", Types.VARCHAR));
        declareParameter(new SqlParameter("end_date", Types.VARCHAR));

        // handling result set
        declareParameter(new SqlReturnResultSet("calendarDays", new RowMapper<CalendarDayBean>() {
            public CalendarDayBean mapRow(ResultSet rs, int i) throws SQLException {
                return new CalendarDayBeanBuilder()


    public Collection<CalendarDayBean> getCalendar(Long personId, LocalDate startDate, LocalDate endDate) {
        final String strStartDate = ...; // convert `startDate` to string
        final String strEndDate = ...; // convert `endDate` to string

        final Map<String, Object> results = execute(personId, strStartDate, strEndDate);
<code>        return ImmutableList.copyOf((List<CalendarDayBean>) results.get("calendarDays"));

The data source is wired into this spring-managed bean. A helper API called getCalendar(...) is created to allow the DAO to invoke this stored procedure.

Now, create a DAO class and wire the stored procedure class into it:-

public class CalendarDAO {
    private final CalendarStoredProcedure calendarStoredProcedure;</code>

    public CalendarDAO(CalendarStoredProcedure calendarStoredProcedure) {
        this.calendarStoredProcedure = calendarStoredProcedure;

    public Collection<CalendarDayBean> getCalendar(final Long personId,
                                                   final LocalDate startDate,
                                                   final LocalDate endDate) {
<code>        return calendarStoredProcedure.getCalendar(personId, startDate, endDate);


Leave a Reply