getResultSet from Spring-JDBC

13,259

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.

Share:
13,259
Dónal
Author by

Dónal

I earn a living by editing text files. I can be contacted at: [email protected] You can find out about all the different kinds of text files I've edited at: My StackOverflow Careers profile

Updated on September 14, 2022

Comments

  • Dónal
    Dónal over 1 year

    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