Results 1 to 5 of 5

Thread: JDBCTemplate query method for StoredProcs?

  1. #1
    Join Date
    Mar 2005
    Posts
    9

    Default JDBCTemplate query method for StoredProcs?

    I noticed all the query methods on JdbcTemplate take PreparedStatementCreators or Strings (for SQL). Why aren't there any query methods that take a CallableStatementCreator and a RowMapper, for example. We use stored procedures exclusively, and nearly all of them return a ResultSet with no other outparameters. Am I missing the easy way to do this in Spring?

    Thanks for any help.

    - Dan

    (The decision to use stored procedures in this manner is beyond my ability to control, so suggestions to use PreparedStatements, etc, are non-starters for me.)

  2. #2
    Join Date
    Feb 2005
    Location
    Boston, MA
    Posts
    1,142

    Default

    Given a JdbcTemplate you can use executeQuery(CallableStatementCreator csc, CallableStatementCallback action). Either as a utility method or a method on a JdbcTemplate subclass. The follow is an example of a utility method.

    Code:
    public List SimpleProc(JdbcTemplate jdbcTemplate, CallableStatementCreator csc, final RowMapper rm)
    {
        return (List)jdbcTemplate.executeQuery(csc, new CallableStatementCallback() {
          public Object doInCallableStatement(CallableStatement cs) throws SQLException {
              ResultSet rs = cs.executeQuery();
              int rowNum = 0;
              List result = new ArrayList();
              while (rs.next()) {
                rowNum++;
                result.add(rm.mapRow(rs, rowNum));
              }
              return result;
          }
        });
    }

  3. #3
    Join Date
    Mar 2005
    Posts
    9

    Default

    Thanks! It looks obvious now that you spell it out, but I wasn't grasping the use of the CallableStatementCallback interface.

  4. #4
    Join Date
    Jan 2005
    Location
    Edinburgh
    Posts
    10

    Default Simpler way for a single result set (Sybase and MS SQL)

    I haven't a Sybase server to try this out on at the moment, but I think this might work...

    If a sybase (MsSQL?) stored procedure just returns a single result set via a select will the following simpler call work?

    List rows = jdbcTemplate.queryForList("exec somedb.db.procname ?, ?", , new Object[] {new Integer(123), "Spain"});

  5. #5
    Join Date
    Jan 2005
    Location
    Edinburgh
    Posts
    10

    Default Simpler way for a single result set (Sybase and MS SQL)

    That should have read
    List rows = jdbcTemplate.queryForList("exec somedb.dbo.procname ?, ?", , new Object[] {new Integer(123), "Spain"});

Similar Threads

  1. PerformanceMonitorInceptor
    By chenrici in forum AOP
    Replies: 15
    Last Post: May 18th, 2006, 04:28 PM
  2. Order of Bean definitions matters?
    By cfuser in forum Container
    Replies: 2
    Last Post: Oct 21st, 2005, 10:29 AM
  3. Spring container fails with no exception
    By naor in forum Container
    Replies: 9
    Last Post: Oct 1st, 2005, 03:39 PM
  4. EHCaching Hibernate
    By dencamel in forum Data
    Replies: 3
    Last Post: Sep 6th, 2005, 09:03 PM
  5. PerformanceMonitorInterceptor
    By tnist in forum AOP
    Replies: 3
    Last Post: Aug 24th, 2005, 01:39 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •