简体   繁体   English

来自Spring-JDBC的getResultSet

[英]getResultSet from Spring-JDBC

I'm using Spring's support for JDBC. 我正在使用Spring对JDBC的支持。 I'd like to use JdbcTemplate (or SimpleJdbcTemplate) to execute a query and obtain the result as an instance of ResultSet. 我想使用JdbcTemplate (或SimpleJdbcTemplate)来执行查询并获取结果作为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? 这种方法的一个明显缺点是它需要我做一个关于SqlRowSet的实现类型的假设(通过强制转换),但是有更好的方法吗?

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. 我想要将结果作为ResultSet而不是bean的集合获取的原因是因为结果将直接传递给Jasper报告以供显示。 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. 换句话说,除了在ResultSet中临时存储每一行​​之外,Java bean只能用于其他任何内容,并且我希望尽可能避免为每个Jasper报告创建这样的bean。

Cheers, Don 干杯,唐

If you want to just perform a query and get the results, why don't you use plain jdbc and grab the resultset? 如果您只想执行查询并获得结果,为什么不使用普通的jdbc并获取结果集? 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. 此外,通过将对象用作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. 即使您的数据访问或报表工具发生更改,您也无需更改DTO类(假设您开始使用xquery而不是jdbc,或者使用apache-poi而不是jasper)。

You can either invoke Jasper inside a JdbcTemplate callback (like a ResultSetExtractor) or use straight JDBC to pass the ResultSet to Jasper. 您可以在JdbcTemplate回调中调用Jasper(如ResultSetExtractor),也可以使用直接JDBC将ResultSet传递给Jasper。 Either way when you call Jasper your connection to the database is still active until your report is finished. 无论哪种方式,当您调用Jasper时,您的数据库连接仍然有效,直到您的报告完成。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM