GetResultSet from Spring -JDBC

I am using Spring support for JDBC. I would like to use JdbcTemplate (or SimpleJdbcTemplate) to execute the query and get the result as an instance of ResultSet.

The only way I can figure this out is to use:

String sql = "select * from...."; SqlRowSet results = jdbcTemplate.queryForRowSet(sql); ((ResultSetWrappingSqlRowSet) results).getResultSet(); 

The obvious drawback of this approach is that it requires me to make an assumption (by casting) about the type of implementation of SqlRowSet, but is there a better way?

Background information ...

The reason I want to get the results as a ResultSet, rather than a beans set, is because the results will be passed directly to the Jasper report for display. In other words, the Java bean will be used as a temporary storage of each row in the ResultSet, and I would like to avoid creating such a bean for each Jasper report, if possible.

Cheers Don

+4
source share
2 answers

If you just want to execute the query and get the results, why don't you use simple jdbc and grab the result set? Note that you do not need spring to do this.

  Connection c = ... c.prepareCall("select ...").getResultSet(); 

In addition, you gain an advantage by using an object like a DTO. You do not need to change your DTO class, even if your data does not work or your reporting tool changes (let's say you start using xquery instead of jdbc or use apache-poi instead of jasper.

+1
source

You can call Jasper inside the JdbcTemplate callback (e.g. ResultSetExtractor) or use direct JDBC to pass the ResultSet to Jasper. In any case, when you call Jasper, your database connection is still active until your report is complete.

0
source

All Articles