<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Spring JDBC &#8211; My Shitty Code</title>
	<atom:link href="https://myshittycode.com/tag/spring-jdbc/feed/" rel="self" type="application/rss+xml" />
	<link>https://myshittycode.com</link>
	<description>Embracing the Messiness in Search of Epic Solutions</description>
	<lastBuildDate>Fri, 06 Jan 2023 16:52:56 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.1</generator>

<image>
	<url>https://myshittycode.com/wp-content/uploads/2022/04/cropped-icon-32x32.png</url>
	<title>Spring JDBC &#8211; My Shitty Code</title>
	<link>https://myshittycode.com</link>
	<width>32</width>
	<height>32</height>
</image> 
<site xmlns="com-wordpress:feed-additions:1">205304208</site>	<item>
		<title>Spring: Invoking Stored Procedure</title>
		<link>https://myshittycode.com/2014/08/01/spring-invoking-stored-procedure/</link>
					<comments>https://myshittycode.com/2014/08/01/spring-invoking-stored-procedure/#respond</comments>
		
		<dc:creator><![CDATA[Shitty Author]]></dc:creator>
		<pubDate>Fri, 01 Aug 2014 14:49:17 +0000</pubDate>
				<category><![CDATA[Programming Language]]></category>
		<category><![CDATA[Spring]]></category>
		<category><![CDATA[Spring JDBC]]></category>
		<guid isPermaLink="false">http://myshittycode.com/?p=529</guid>

					<description><![CDATA[<p>PROBLEM There are many ways to skin a cat&#8230; so is invoking a stored procedure using Spring. Let&#8217;s assume we want to invoke the following stored procedure that accepts 3 arguments (person ID, start date, end date):- This stored procedure returns a result set where each row contains a person ID, a date and a [&#8230;]</p>
<p>The post <a rel="nofollow" href="https://myshittycode.com/2014/08/01/spring-invoking-stored-procedure/">Spring: Invoking Stored Procedure</a> appeared first on <a rel="nofollow" href="https://myshittycode.com">My Shitty Code</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<h2 class="wp-block-heading">PROBLEM</h2>



<p>There are many ways to skin a cat&#8230; so is invoking a stored procedure using Spring.</p>



<p>Let&#8217;s assume we want to invoke the following stored procedure that accepts 3 arguments (person ID, start date, end date):-</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: plain; title: ; notranslate">
{ call calendar_sp (?, ?, ?) }
</pre></div>


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



<h2 class="wp-block-heading">SOLUTION</h2>



<p>Create a class that extends <code>org.springframework.jdbc.object.StoredProcedure</code>:-</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: java; title: ; notranslate">
@Component
public class CalendarStoredProcedure extends StoredProcedure {&lt;/code&gt;
&lt;code&gt;

    @Autowired
    public CalendarStoredProcedure(DataSource dataSource) {
        super(dataSource, &quot;calendar_sp&quot;);

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

        // handling result set
        declareParameter(new SqlReturnResultSet(&quot;calendarDays&quot;, new RowMapper&lt;CalendarDayBean&gt;() {
            @Override
            public CalendarDayBean mapRow(ResultSet rs, int i) throws SQLException {
                return new CalendarDayBeanBuilder()
                        .setPersonId(rs.getLong(&quot;person_id&quot;))
                        .setDate(CommonUtils.convertSQLDateToLocalDate(rs.getDate(&quot;calendar_date&quot;)))
                        .setDescription(rs.getString(&quot;description&quot;))
                        .createCalendarDayBean();
            }
        }));

        compile();
    }

    @SuppressWarnings(&quot;unchecked&quot;)
    public Collection&lt;CalendarDayBean&gt; getCalendar(Long personId, LocalDate startDate, LocalDate endDate) {
        final String strStartDate = ...; // convert `startDate` to string
        final String strEndDate = ...; // convert `endDate` to string

        final Map&lt;String, Object&gt; results = execute(personId, strStartDate, strEndDate);
&lt;/code&gt;
&lt;code&gt;        return ImmutableList.copyOf((List&lt;CalendarDayBean&gt;) results.get(&quot;calendarDays&quot;));
    }
}
</pre></div>


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



<p>Now, create a DAO class and wire the stored procedure class into it:-</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: java; title: ; notranslate">
@Service
public class CalendarDAO {
    private final CalendarStoredProcedure calendarStoredProcedure;&lt;/code&gt;
&lt;code&gt;

    @Autowired
    public CalendarDAO(CalendarStoredProcedure calendarStoredProcedure) {
        this.calendarStoredProcedure = calendarStoredProcedure;
    }

    @Override
    public Collection&lt;CalendarDayBean&gt; getCalendar(final Long personId,
                                                   final LocalDate startDate,
                                                   final LocalDate endDate) {
&lt;/code&gt;
&lt;code&gt;        return calendarStoredProcedure.getCalendar(personId, startDate, endDate);
    }
}
</pre></div><p>The post <a rel="nofollow" href="https://myshittycode.com/2014/08/01/spring-invoking-stored-procedure/">Spring: Invoking Stored Procedure</a> appeared first on <a rel="nofollow" href="https://myshittycode.com">My Shitty Code</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://myshittycode.com/2014/08/01/spring-invoking-stored-procedure/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">529</post-id>	</item>
	</channel>
</rss>

<!--
Performance optimized by W3 Total Cache. Learn more: https://www.boldgrid.com/w3-total-cache/?utm_source=w3tc&utm_medium=footer_comment&utm_campaign=free_plugin

Page Caching using Disk: Enhanced 
Lazy Loading (feed)
Database Caching 56/67 queries in 0.035 seconds using Disk

Served from: myshittycode.com @ 2026-02-21 15:40:34 by W3 Total Cache
-->