alexcuesta

My tech blog

Reading a ResultSet returned by a Stored Procedure

leave a comment »

Declare the stored procedure to return a resultset:

private final SimpleJdbcCall myStoredProcedure;
            
public MyRepositoryJdbc(DataSource dataSource) {       
  this.MyGetStoredProcedure = new SimpleJdbcCall(dataSource)
                                .withProcedureName("MyStoredProcedure")
                                .returningResultSet("items", new MyRowMapper());
}

Create the corresponding RowMapper to map each row of the resultset to the a Domain class:

class MyRowMapper implements RowMapper<DomainClass> {

@Override
public DomainClass mapRow(ResultSet rs, int rowNum) throws SQLException {
            DomainClass myclass= new DomainClass ();
                  
            myclass.setId(rs.getLong("id"));
            myclass.setSomething(rs.getString("something"));                      
            
            return myclass;
      }
      
}

Call the Stored Procedure:

public DomainClass findById(String id) {

      SqlParameterSource in = new MapSqlParameterSource()               
            .addValue("id", customerId);
      
      Map<String, Object> out = myStoredProcedure.execute(in);

      return ((List<DomainClass>) out.get("items")).get(0);             
}

Written by alexcuesta

August 10, 2010 at 9:48 am

Posted in Java

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: