如何通过主报告的参数将结果集传递给子报表?

时间:2012-10-26 04:48:36

标签: java jasper-reports subreport

我是 JasperReports 的新手。

我有一个结果集作为参数传递给主报表。在主报告中,我需要将其作为数据源传递给我的子报告

param = super.getParam(param, "MY_RESULT_SET", resultSet);
JasperPrint jasperPrint = JasperFillManager.fillReport("myJasper.jasper", param, dataSource);

来自 myJasper.jrxml 的片段:

<parameter name="MY_RESULT_SET" class="java.sql.ResultSet"/>
<subreport>
    <reportElement x="285" y="0" width="270" height="20"/>              
    <dataSourceExpression><![CDATA[$P{MY_RESULT_SET}]]></dataSourceExpression>
    <subreportExpression><![CDATA["mySubreport.jasper"]]></subreportExpression>
</subreport>

如上所述,我得到以下例外

java.lang.ClassCastException: com.mysql.jdbc.JDBC4ResultSet cannot be cast to net.sf.jasperreports.engine.JRDataSource
at net.sf.jasperreports.engine.fill.JRFillSubreport.evaluateSubreport(JRFillSubreport.java:356)
at net.sf.jasperreports.engine.fill.JRFillSubreport.evaluate(JRFillSubreport.java:275)
at net.sf.jasperreports.engine.fill.JRFillElementContainer.evaluate(JRFillElementContainer.java:257)
at net.sf.jasperreports.engine.fill.JRFillBand.evaluate(JRFillBand.java:473)
at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillColumnBand(JRVerticalFiller.java:2021)
at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillDetail(JRVerticalFiller.java:755)
at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillReportStart(JRVerticalFiller.java:265)
at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillReport(JRVerticalFiller.java:128)
at net.sf.jasperreports.engine.fill.JRBaseFiller.fill(JRBaseFiller.java:836)
at net.sf.jasperreports.engine.fill.JRBaseFiller.fill(JRBaseFiller.java:765)
at net.sf.jasperreports.engine.fill.JRFiller.fillReport(JRFiller.java:84)
at net.sf.jasperreports.engine.JasperFillManager.fillReport(JasperFillManager.java:624)
at net.sf.jasperreports.engine.JasperFillManager.fillReport(JasperFillManager.java:540)

有人可以指导我吗?

1 个答案:

答案 0 :(得分:0)

你需要构建一个JRResultSetDataSource并传递它。这应该像添加:

一样简单
JRDataSource myResultSetDS = new JRResultSetDataSource(resultSet)

然后将myResultSetDS作为参数而不是resultSet本身。