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

Asked by: Roland207 | Posted: 28-01-2022

Answer 1

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.

Answered by: Maria847 | Posted: 01-03-2022

Answer 2

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.

Answered by: Sydney657 | Posted: 01-03-2022

Similar questions

java - HQL - getResultSet using proxy

I'm trying to execute this command : Query q = sql.get().createQuery("SELECT object(p) from Participation p where p.member.address = :address").setParameter("address", address); List<Participation> participations = q.getResultList(); When I debug the function, the 'getResultList()' passes by this function (from the package org.hibernate.engine.internal;) public Obje...

Still can't find your answer? Check out these amazing Java communities for help...

Java Reddit Community | Java Help Reddit Community | Java Community | Java Discord | Java Programmers (Facebook) | Java developers (Facebook)