当我尝试传递我的报告连接时,为什么 get cannot be cast to net.sf.jasperreports.engine.JRDataSource?

Why do I get cannot be cast to net.sf.jasperreports.engine.JRDataSource when I try to pass my report connection?

我的报告 jrxml 文件看起来像

<band height="256" splitType="Stretch">
            <componentElement>
                <reportElement x="0" y="0" width="555" height="50" uuid="eb5b00f3-c370-4c7f-bb10-b7589ff293ff"/>
                <jr:list xmlns:jr="http://jasperreports.sourceforge.net/jasperreports/components" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports/components http://jasperreports.sourceforge.net/xsd/components.xsd" printOrder="Vertical">
                    <datasetRun subDataset="listDataset" uuid="364ec058-5b7e-43e0-9272-92907dfba7f3">
                        <datasetParameter name="REPORT_DATE">
                            <datasetParameterExpression><![CDATA[$P{REPORT_DATE}]]></datasetParameterExpression>
                        </datasetParameter>
                        <dataSourceExpression><![CDATA[$P{REPORT_CONNECTION}]]></dataSourceExpression>
                    </datasetRun>
                    <jr:listContents height="50" width="555">
                        <staticText>
                            <reportElement x="1" y="2" width="100" height="20" uuid="cd4ed06d-2c04-4342-8cc2-ff7077621eb6"/>
                            <text><![CDATA[LABEL_VALUE]]></text>
                        </staticText>
                        <textField>
                            <reportElement x="101" y="2" width="129" height="20" uuid="d307fa41-0962-4b17-812d-774262e55e9e"/>
                            <textFieldExpression><![CDATA[$F{LABEL_VALUE}]]></textFieldExpression>
                        </textField>
                        <staticText>
                            <reportElement x="230" y="2" width="140" height="20" uuid="20989891-eb66-45ee-8fbb-b7c53a65f0fe"/>
                            <text><![CDATA[ATTRIBUTE_VALUE]]></text>
                        </staticText>
                        <textField>
                            <reportElement x="370" y="2" width="184" height="20" uuid="fa972e53-dc8a-402e-96ad-9428b402ccd8"/>
                            <textFieldExpression><![CDATA[$F{ATTRIBUTE_VALUE}]]></textFieldExpression>
                        </textField>
                    </jr:listContents>
                </jr:list>
            </componentElement>
</band>

但是我得到了一个 ClassCastException,

java.lang.ClassCastException: org.jboss.jca.adapters.jdbc.jdk6.WrappedConnectionJDK6 cannot be cast to net.sf.jasperreports.engine.JRDataSource

我想不通,为什么?

来自 JasperReports api JRDatasetRun

A dataset run declaration supplies the values for the dataset parameters as well as the data source through which the dataset will iterate. Optionally, a java.sql.Connection can be passed to the dataset instead of a JRDataSource instance, when there is a SQL query associated with the dataset. This query is executed by the engine using the JDBC connection and the java.sql.ResultSet object obtained is iterated through.

结论当你运行一个数据集datasetRun你可以:

  1. 使用 dataSourceExpression 标签传递 JRDatasource

  2. 使用 connectionExpression 标签传递 JDBC 连接

  3. 不传递任何内容。

所以你做错了什么?

您在 dataSourceExpression 标记而不是 connectionExpression 标记中传递 JDBC 连接,JasperReports 希望它是 JRDatasource 并尝试将其转换为 JRDatasource。

如果您想传递报告连接,您应该使用 connectionExpression,如下所示。

<datasetRun subDataset="listDataset" uuid="364ec058-5b7e-43e0-9272-92907dfba7f3">
    <datasetParameter name="REPORT_DATE">
       <datasetParameterExpression><![CDATA[$P{REPORT_DATE}]]></datasetParameterExpression>
    </datasetParameter>
    <connectionExpression><![CDATA[$P{REPORT_CONNECTION}]]></connectionExpression>
</datasetRun>