我是 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)
有人可以指导我吗?
答案 0 :(得分:0)
你需要构建一个JRResultSetDataSource
并传递它。这应该像添加:
JRDataSource myResultSetDS = new JRResultSetDataSource(resultSet)
然后将myResultSetDS
作为参数而不是resultSet
本身。