java 从 Spring-JDBC 获取结果集

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/212569/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 11:26:38  来源:igfitidea点击:

getResultSet from Spring-JDBC

javaspringjdbcjasper-reports

提问by Dónal

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.

我正在使用 Spring 对 JDBC 的支持。我想使用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. 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 而不是 bean 集合的形式获取结果的原因是,结果将直接传递给 Jasper 报告进行显示。换句话说,Java bean 只会用于临时存储 ResultSet 中的每一行,如果可能的话,我想避免为每个 Jasper 报告创建这样的 bean。

Cheers, Don

干杯,唐

采纳答案by Miguel Ping

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.

如果您只想执行查询并获取结果,为什么不使用普通的 jdbc 并获取结果集?请注意,您不需要 spring 来执行此操作。

    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.

此外,您可以通过将对象用作 DTO 来获得优势。即使您的数据访问或报告工具发生变化,您也不需要更改 DTO 类(假设您开始使用 xquery 而不是 jdbc,或者您使用 apache-poi 而不是 jasper。

回答by Bill Poitras

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.

您可以在 JdbcTemplate 回调(如 ResultSetExtractor)中调用 Jasper,也可以使用直接 JDBC 将 ResultSet 传递给 Jasper。无论哪种方式,当您调用 Jasper 时,您与数据库的连接仍然处于活动状态,直到您的报告完成。