Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getResultSet from Spring-JDBC

I'm using Spring's support for JDBC. I'd like to use JdbcTemplate (or SimpleJdbcTemplate) to execute a query and obtain the result as an instance of ResultSet.

The only way that I can see of achieving this is using:

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

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

Background info...

The reason I want to obtain the results as a ResultSet, rather than a collection of beans, is because the results will be passed straight to a Jasper report for display. In other words, the Java bean would be used for nothing other than temporarily storing each row in the ResultSet, and I'd like to avoid creating such a bean for every Jasper report if possible.

Cheers, Don

like image 547
Dónal Avatar asked Oct 17 '08 15:10

Dónal


2 Answers

If you want to just perform a query and get the results, why don't you use plain jdbc and grab the resultset? Notice that you don't need spring to do just this.

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

Besides, you get an advantage by using an object as a DTO. You don't need to change your DTO class even if your data acess or your report tool changes (let's say you start using xquery instead of jdbc or you use apache-poi instead of jasper.

like image 180
Miguel Ping Avatar answered Sep 30 '22 06:09

Miguel Ping


You can either invoke Jasper inside a JdbcTemplate callback (like a ResultSetExtractor) or use straight JDBC to pass the ResultSet to Jasper. Either way when you call Jasper your connection to the database is still active until your report is finished.

like image 43
Bill Poitras Avatar answered Sep 30 '22 07:09

Bill Poitras