Spring Framework

org.springframework.jdbc.core
Class RowMapperResultSetExtractor<T>

java.lang.Object
  extended by org.springframework.jdbc.core.RowMapperResultSetExtractor<T>
All Implemented Interfaces:
ResultSetExtractor<java.util.List<T>>

public class RowMapperResultSetExtractor<T>
extends java.lang.Object
implements ResultSetExtractor<java.util.List<T>>

Adapter implementation of the ResultSetExtractor interface that delegates to a RowMapper which is supposed to create an object for each row. Each object is added to the results List of this ResultSetExtractor.

Useful for the typical case of one object per row in the database table. The number of entries in the results list will match the number of rows.

Note that a RowMapper object is typically stateless and thus reusable; just the RowMapperResultSetExtractor adapter is stateful.

A usage example with JdbcTemplate:

JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);  // reusable object
 RowMapper rowMapper = new UserRowMapper();  // reusable object

 List allUsers = (List) jdbcTemplate.query(
     "select * from user",
     new RowMapperResultSetExtractor(rowMapper, 10));

 User user = (User) jdbcTemplate.queryForObject(
     "select * from user where id=?", new Object[] {id},
     new RowMapperResultSetExtractor(rowMapper, 1));

Alternatively, consider subclassing MappingSqlQuery from the jdbc.object package: Instead of working with separate JdbcTemplate and RowMapper objects, you can have executable query objects (containing row-mapping logic) there.

Since:
1.0.2
Author:
Juergen Hoeller
See Also:
RowMapper, JdbcTemplate, MappingSqlQuery

Constructor Summary
RowMapperResultSetExtractor(RowMapper<T> rowMapper)
          Create a new RowMapperResultSetExtractor.
RowMapperResultSetExtractor(RowMapper<T> rowMapper, int rowsExpected)
          Create a new RowMapperResultSetExtractor.
 
Method Summary
 java.util.List<T> extractData(java.sql.ResultSet rs)
          Implementations must implement this method to process the entire ResultSet.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

RowMapperResultSetExtractor

public RowMapperResultSetExtractor(RowMapper<T> rowMapper)
Create a new RowMapperResultSetExtractor.

Parameters:
rowMapper - the RowMapper which creates an object for each row

RowMapperResultSetExtractor

public RowMapperResultSetExtractor(RowMapper<T> rowMapper,
                                   int rowsExpected)
Create a new RowMapperResultSetExtractor.

Parameters:
rowMapper - the RowMapper which creates an object for each row
rowsExpected - the number of expected rows (just used for optimized collection handling)
Method Detail

extractData

public java.util.List<T> extractData(java.sql.ResultSet rs)
                              throws java.sql.SQLException
Description copied from interface: ResultSetExtractor
Implementations must implement this method to process the entire ResultSet.

Specified by:
extractData in interface ResultSetExtractor<java.util.List<T>>
Parameters:
rs - ResultSet to extract data from. Implementations should not close this: it will be closed by the calling JdbcTemplate.
Returns:
an arbitrary result object, or null if none (the extractor will typically be stateful in the latter case).
Throws:
java.sql.SQLException - if a SQLException is encountered getting column values or navigating (that is, there's no need to catch SQLException)

Spring Framework