Is there a way to return a "normal" resultset instead of a rowset from a NamedParameterJdbcTemplate
? The rowset is using too much memory.
I am creating an application to query a database, returning the results as an excel file. The results can be up to a million rows with 20 columns.
I use the NamedParameterJdbcTemplate
class to avoid the use of '?', which is really neat.
This my code:
NamedParameterJdbcTemplate jdbcTemplate = new NamedParameterJdbcTemplate(dataSource);
MapSqlParameterSource paramSource = new MapSqlParameterSource();
paramSource.addValue("name", name);
writeExcel(jdbcTemplate.queryForRowSet(pQuery, paramSource), pOutputStream);
However, the queryForRowset
reads the entire rowset into memory before proceeding. In classical JDBC, you would just open a resultset and read row by row, keeping the connection open but saving memory.
You can use jdbcTemplate
's method
public <T> T query(final String sql, final ResultSetExtractor<T> rse)
and pass your own ResultSetExtractor