JasperReports:(SubReport)提供的java.sql.Connection对象为null

时间:2016-03-22 11:53:01

标签: java jasper-reports subreport

我使用JesperRports在其中创建了一个包含SubReport的报告,但它不起作用且代码看起来不错。

我得到的警告是:The supplied java.sql.Connection object is null

这是我的完整代码:

MasterTeste.jrxml

    <?xml version="1.0" encoding="UTF-8"?>
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="MasterTeste" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="e976d12d-4c1b-4cfd-900a-6ac502f80b50">
    <parameter name="DS" class="java.util.List">
        <defaultValueExpression><![CDATA[$F{lstCours}]]></defaultValueExpression>
    </parameter>
    <queryString>
        <![CDATA[]]>
    </queryString>
    <field name="nom" class="java.lang.String">
        <fieldDescription><![CDATA[nom]]></fieldDescription>
    </field>
    <field name="progres" class="java.lang.String">
        <fieldDescription><![CDATA[progres]]></fieldDescription>
    </field>
    <field name="score" class="java.lang.String">
        <fieldDescription><![CDATA[score]]></fieldDescription>
    </field>
    <field name="lstCours" class="java.util.List">
        <fieldDescription><![CDATA[lstCours]]></fieldDescription>
    </field>
    <detail>
        <band height="180" splitType="Stretch">
            <staticText>
                <reportElement mode="Opaque" x="0" y="3" width="555" height="26" backcolor="#666666" uuid="06a43d96-317c-4a59-bfae-d014050e3955"/>
                <text><![CDATA[]]></text>
            </staticText>
            <staticText>
                <reportElement x="3" y="6" width="65" height="20" uuid="7df685f6-8e54-4147-ac6e-52eae09577c0"/>
                <textElement>
                    <font size="15" isBold="true"/>
                </textElement>
                <text><![CDATA[Section : ]]></text>
            </staticText>
            <staticText>
                <reportElement x="256" y="6" width="68" height="20" uuid="7cbd59f2-280d-4e8f-84db-5a7bf0425596"/>
                <textElement>
                    <font size="15" isBold="true"/>
                </textElement>
                <text><![CDATA[Progres : ]]></text>
            </staticText>
            <staticText>
                <reportElement x="434" y="6" width="60" height="20" uuid="b666bed5-48c7-4edb-ae70-5b582587e4f6"/>
                <textElement>
                    <font size="15" isBold="true"/>
                </textElement>
                <text><![CDATA[Score :]]></text>
            </staticText>
            <textField>
                <reportElement x="71" y="6" width="178" height="20" uuid="cb1fa244-3296-40d3-afd9-ee28d29c798d"/>
                <textElement>
                    <font size="13"/>
                </textElement>
                <textFieldExpression><![CDATA[$F{nom}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="329" y="6" width="57" height="20" uuid="4d667ac4-cd25-4f1f-a1a5-5b354ccc00b9"/>
                <textElement>
                    <font size="13"/>
                </textElement>
                <textFieldExpression><![CDATA[$F{progres}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="500" y="6" width="55" height="20" uuid="3e771498-10a4-4f4c-9305-cd639de1b4e5"/>
                <textElement>
                    <font size="13"/>
                </textElement>
                <textFieldExpression><![CDATA[$F{score}]]></textFieldExpression>
            </textField>
            <subreport>
                <reportElement positionType="Float" x="0" y="36" width="555" height="140" isRemoveLineWhenBlank="true" backcolor="#99CCFF" uuid="33184f19-8225-428f-88ba-a488753fd445"/>
                <subreportParameter name="DS1">
                    <subreportParameterExpression><![CDATA[new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($P{DS})]]></subreportParameterExpression>
                </subreportParameter>
                <subreportExpression><![CDATA["C:\\reports\\" + "teste.jasper"]]></subreportExpression>
            </subreport>
        </band>
    </detail>
</jasperReport>

Teste.jrxml(子报告):

<?xml version="1.0" encoding="UTF-8"?>
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="hello" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="15289120-bdf0-4edd-905c-647999c3c0cc">
    <style name="table">
        <box>
            <pen lineWidth="1.0" lineColor="#000000"/>
        </box>
    </style>
    <style name="table_TH" mode="Opaque" backcolor="#00FFFF">
        <box>
            <topPen lineWidth="0.5" lineColor="#000000"/>
            <bottomPen lineWidth="0.5" lineColor="#000000"/>
        </box>
    </style>
    <style name="table_CH" mode="Opaque" backcolor="#C6C6C6">
        <box>
            <topPen lineWidth="0.5" lineColor="#000000"/>
            <bottomPen lineWidth="0.5" lineColor="#000000"/>
        </box>
    </style>
    <style name="table_TD" mode="Opaque" backcolor="#FFFFFF">
        <box>
            <topPen lineWidth="0.5" lineColor="#000000"/>
            <bottomPen lineWidth="0.5" lineColor="#000000"/>
        </box>
    </style>
    <subDataset name="New Dataset 1" uuid="2c012e5d-0895-4ef1-8b47-20fd8c6b281f">
        <queryString>
            <![CDATA[]]>
        </queryString>
        <field name="nom" class="java.lang.String"/>
        <field name="introComp" class="java.lang.String"/>
        <field name="lessComp" class="java.lang.String"/>
        <field name="resumComp" class="java.lang.String"/>
        <field name="quiz" class="java.lang.String"/>
        <field name="progres" class="java.lang.String"/>
    </subDataset>
    <parameter name="DS1" class="net.sf.jasperreports.engine.data.JRBeanCollectionDataSource"/>
    <queryString>
        <![CDATA[]]>
    </queryString>
    <detail>
        <band height="151" splitType="Stretch">
            <componentElement>
                <reportElement key="table" style="table" x="1" y="4" width="555" height="140" isPrintWhenDetailOverflows="true" uuid="1e15cf18-8adf-4bad-9339-04deb8240c82"/>
                <jr:table xmlns:jr="http://jasperreports.sourceforge.net/jasperreports/components" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports/components http://jasperreports.sourceforge.net/xsd/components.xsd">
                    <datasetRun subDataset="New Dataset 1" uuid="5ccdd061-c1c3-46a0-bc92-af14bda9b14a">
                        <datasetParameter name="REPORT_DATA_SOURCE">
                            <datasetParameterExpression><![CDATA[$P{DS1}]]></datasetParameterExpression>
                        </datasetParameter>
                    </datasetRun>
                    <jr:column width="99" uuid="faddf5bb-759d-4efc-9a3e-9cf16b8cbda2">
                        <jr:columnHeader style="table_CH" height="35" rowSpan="1">
                            <staticText>
                                <reportElement x="0" y="4" width="56" height="20" uuid="795dac43-0ff0-482c-89a0-7dac3b27d513"/>
                                <textElement>
                                    <font size="15" isBold="true"/>
                                </textElement>
                                <text><![CDATA[Cours]]></text>
                            </staticText>
                        </jr:columnHeader>
                        <jr:detailCell style="table_TD" height="30" rowSpan="1">
                            <textField>
                                <reportElement x="3" y="4" width="56" height="20" uuid="195d51a0-9e45-4201-ad67-d3026ce2e72c"/>
                                <textFieldExpression><![CDATA[$F{nom}]]></textFieldExpression>
                            </textField>
                        </jr:detailCell>
                    </jr:column>
                    <jr:column width="103" uuid="8a8bc20b-f8b4-45a6-9cbd-68b4a7391426">
                        <jr:columnHeader style="table_CH" height="35" rowSpan="1">
                            <staticText>
                                <reportElement x="0" y="5" width="90" height="20" uuid="795dac43-0ff0-482c-89a0-7dac3b27d513"/>
                                <textElement>
                                    <font size="15" isBold="true"/>
                                </textElement>
                                <text><![CDATA[Introduction]]></text>
                            </staticText>
                        </jr:columnHeader>
                        <jr:detailCell style="table_TD" height="30" rowSpan="1">
                            <textField>
                                <reportElement x="3" y="4" width="74" height="20" uuid="195d51a0-9e45-4201-ad67-d3026ce2e72c"/>
                                <textFieldExpression><![CDATA[$F{introComp}]]></textFieldExpression>
                            </textField>
                        </jr:detailCell>
                    </jr:column>
                    <jr:column width="87" uuid="fde36038-294c-4987-bd03-734736ddb792">
                        <jr:columnHeader style="table_CH" height="35" rowSpan="1">
                            <staticText>
                                <reportElement x="0" y="5" width="67" height="20" uuid="795dac43-0ff0-482c-89a0-7dac3b27d513"/>
                                <textElement>
                                    <font size="15" isBold="true"/>
                                </textElement>
                                <text><![CDATA[Lesson]]></text>
                            </staticText>
                        </jr:columnHeader>
                        <jr:detailCell style="table_TD" height="30" rowSpan="1">
                            <textField>
                                <reportElement x="3" y="4" width="74" height="20" uuid="195d51a0-9e45-4201-ad67-d3026ce2e72c"/>
                                <textFieldExpression><![CDATA[$F{lessComp}]]></textFieldExpression>
                            </textField>
                        </jr:detailCell>
                    </jr:column>
                    <jr:column width="90" uuid="51b5dbd4-1bf2-450f-ba8c-f75ea831860b">
                        <jr:columnHeader style="table_CH" height="35" rowSpan="1">
                            <staticText>
                                <reportElement x="0" y="5" width="67" height="20" uuid="795dac43-0ff0-482c-89a0-7dac3b27d513"/>
                                <textElement>
                                    <font size="15" isBold="true"/>
                                </textElement>
                                <text><![CDATA[Resume]]></text>
                            </staticText>
                        </jr:columnHeader>
                        <jr:detailCell style="table_TD" height="30" rowSpan="1">
                            <textField>
                                <reportElement x="3" y="4" width="74" height="20" uuid="195d51a0-9e45-4201-ad67-d3026ce2e72c"/>
                                <textFieldExpression><![CDATA[$F{resumComp}]]></textFieldExpression>
                            </textField>
                        </jr:detailCell>
                    </jr:column>
                    <jr:column width="87" uuid="657d2155-3e2f-4f41-ba0a-a17a76598491">
                        <jr:columnHeader style="table_CH" height="35" rowSpan="1">
                            <staticText>
                                <reportElement x="0" y="5" width="67" height="20" uuid="795dac43-0ff0-482c-89a0-7dac3b27d513"/>
                                <textElement>
                                    <font size="15" isBold="true"/>
                                </textElement>
                                <text><![CDATA[Quiz]]></text>
                            </staticText>
                        </jr:columnHeader>
                        <jr:detailCell style="table_TD" height="30" rowSpan="1">
                            <textField>
                                <reportElement x="3" y="4" width="56" height="20" uuid="195d51a0-9e45-4201-ad67-d3026ce2e72c"/>
                                <textFieldExpression><![CDATA[$F{quiz}]]></textFieldExpression>
                            </textField>
                        </jr:detailCell>
                    </jr:column>
                    <jr:column width="87" uuid="d36e39c4-8408-4f2f-865f-787aa3166a7e">
                        <jr:columnHeader style="table_CH" height="35" rowSpan="1">
                            <staticText>
                                <reportElement x="0" y="5" width="67" height="20" uuid="795dac43-0ff0-482c-89a0-7dac3b27d513"/>
                                <textElement>
                                    <font size="15" isBold="true"/>
                                </textElement>
                                <text><![CDATA[Progres]]></text>
                            </staticText>
                        </jr:columnHeader>
                        <jr:detailCell style="table_TD" height="30" rowSpan="1">
                            <textField>
                                <reportElement x="3" y="4" width="56" height="20" uuid="195d51a0-9e45-4201-ad67-d3026ce2e72c"/>
                                <textFieldExpression><![CDATA[$F{progres}]]></textFieldExpression>
                            </textField>
                        </jr:detailCell>
                    </jr:column>
                </jr:table>
            </componentElement>
        </band>
    </detail>
</jasperReport>

Java:

    public static void main(String[] args) {
        List<CoursRapport> coursRapports = new ArrayList<CoursRapport>();
        coursRapports.add(new CoursRapport("cours 1", "Complet", "Complet", "Complet", "Complet", "100 %"));
        coursRapports.add(new CoursRapport("cours 2", "Complet", "Complet", "Complet", "Complet", "100 %"));
        coursRapports.add(new CoursRapport("cours 3", "Complet", "Complet", "Complet", "Complet", "100 %"));

        SectionRapport sectRapport = new SectionRapport();

        sectRapport.setNom("Sect 1");
        sectRapport.setProgres("100%");
        sectRapport.setScore("80%");
        sectRapport.setLstCours(coursRapports);

        List<SectionRapport> lstSectionRapports = new ArrayList<SectionRapport>();
        lstSectionRapports.add(sectRapport);

        try{               
            JasperReport jasperReport = JasperCompileManager.compileReport("C:\\reports\\MasterTeste.jrxml");   
            JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, new HashMap<String, Object>(), new JRBeanCollectionDataSource(lstSectionRapports));      // new JREmptyDataSource()
            JasperExportManager.exportReportToPdfFile(jasperPrint, "C:\\ressources\\teste.pdf");
            System.out.println("HelloJasper.pdf has been generated!");
        } catch (JRException e){
            e.printStackTrace();
        }   
}

当我使用样本报告时它工作正常,但现在subreport部分因上面提到的警告而空了,所以也许我没有正确实现subreport? / p>

1 个答案:

答案 0 :(得分:1)

在子报表中设置whenNoDataType =“AllSectionsNoDetail”(在jasperReport元素上),并将表格元素移动到标题或摘要带。

或者,您可以将新的JREmptyDataSource()作为主报表中子报表的数据源表达式传递。