title: Spring JDBC Template for calling Stored Procedures
body: <p>What is the correct way to invoke stored procedures using modern day (circa 2012) Spring JDBC Template?</p>
  
  <p>Say, I have a stored procedure that declares both <code>IN</code> and <code>OUT</code> parameters, something like this:</p>
  
  <pre><code>mypkg.doSomething(
      id OUT int,
      name IN String,
      date IN Date
  )
  </code></pre>
  
  <p>I have come across <code>CallableStatementCreator</code> based approaches where we have to explicitly register <code>IN</code> and <code>OUT</code> parameters. Consider the following method in <code>JdbcTemplate</code> class:</p>
  
  <pre><code>public Map&lt;String, Object&gt; call(CallableStatementCreator csc, List&lt;SqlParameter&gt; declaredParameters)
  </code></pre>
  
  <p>Of course, I do know that I can use it like so:</p>
  
  <pre><code>List&lt;SqlParameter&gt; declaredParameters = new ArrayList&lt;SqlParameter&gt;();
  
  declaredParameters.add(new SqlOutParameter("id", Types.INTEGER));
  declaredParameters.add(new SqlParameter("name", Types.VARCHAR));
  declaredParameters.add(new SqlParameter("date", Types.DATE));
  
  this.jdbcTemplate.call(new CallableStatementCreator() {
  
      @Override
      CallableStatement createCallableStatement(Connection con) throws SQLException {
          CallableStatement stmnt = con.createCall("{mypkg.doSomething(?, ?, ?)}");
  
          stmnt.registerOutParameter("id", Types.INTEGER);
          stmnt.setString("name", "&lt;name&gt;");
          stmnt.setDate("date", &lt;date&gt;);
  
          return stmnt;
      }
  }, declaredParameters);
  </code></pre>
  
  <p>What is the purpose of <code>declaredParameters</code> when I am already registering them in my <code>csc</code> implementation? In other words, why would I need to pass in a <code>csc</code> when spring can simply do <code>con.prepareCall(sql)</code> internally? Basically, can't I pass in either one of them instead of both of them?</p>
  
  <p>Or, is there a much better way to call stored procedures (using Spring JDBC Template) than what I have come across so far?</p>
  
  <p><em>Note:</em> You may find many questions that appear to have a similar title but they are not the same as this one.</p>