Why is my subdataset blank when I run report from Java code? [duplicate]

随声附和 提交于 2021-01-28 08:41:54

问题


I created a table design using Jaspersoft Studio. When I am using this jrxml in Java no table is displayed

I am using Jaspersoft Studio as a plugin in Eclipse to generate jrxml. While previewing the table gets displayed in JSS, but when I am using Java code to export it into pdf/xls/docx its not getting loaded. Empty file without any data.

This is my JRXML file.

<?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="Blank_A4_Landscape" pageWidth="802" pageHeight="555" orientation="Landscape" columnWidth="802" leftMargin="0" rightMargin="0" topMargin="0" bottomMargin="0" uuid="f391983e-5ad3-4a64-b310-fc7f3426e3f0">
    <property name="com.jaspersoft.studio.data.defaultdataadapter" value="Oracle"/>
    <style name="Table_CH" mode="Opaque" backcolor="#BFE1FF">
        <box>
            <pen lineWidth="0.5" lineColor="#000000"/>
            <topPen lineWidth="0.5" lineColor="#000000"/>
            <leftPen lineWidth="0.5" lineColor="#000000"/>
            <bottomPen lineWidth="0.5" lineColor="#000000"/>
            <rightPen lineWidth="0.5" lineColor="#000000"/>
        </box>
    </style>
    <style name="Table_TD" mode="Opaque" backcolor="#FFFFFF">
        <box>
            <pen lineWidth="0.5" lineColor="#000000"/>
            <topPen lineWidth="0.5" lineColor="#000000"/>
            <leftPen lineWidth="0.5" lineColor="#000000"/>
            <bottomPen lineWidth="0.5" lineColor="#000000"/>
            <rightPen lineWidth="0.5" lineColor="#000000"/>
        </box>
        <conditionalStyle>
            <conditionExpression><![CDATA[new Boolean($V{REPORT_COUNT}.intValue()%2==0)]]></conditionExpression>
            <style backcolor="#E3F1FF"/>
        </conditionalStyle>
    </style>
    <subDataset name="table" uuid="9fa2cf87-1e9b-4673-a13c-55f4b337d4db">
        <property name="com.jaspersoft.studio.data.defaultdataadapter" value="Oracle"/>
        <queryString language="SQL">
            <![CDATA[select * from CODE]]>
        </queryString>
        <field name="NAME" class="java.lang.String"/>
        <field name="MOBILE" class="java.lang.String"/>
        <field name="COMPANY_NAME" class="java.lang.String"/>
        <field name="SITE_ID" class="java.lang.String"/>
        <field name="SITE_NAME" class="java.lang.String"/>
        <field name="IN_TIME" class="java.lang.String"/>
        <field name="OUT_TIME" class="java.lang.String"/>
        <field name="CREATE_ON" class="java.lang.String"/>
    </subDataset>
    <queryString language="SQL">
        <![CDATA[select * from CODE]]>
    </queryString>
    <field name="NAME" class="java.lang.String"/>
    <field name="MOBILE" class="java.lang.String"/>
    <field name="COMPANY_NAME" class="java.lang.String"/>
    <field name="SITE_ID" class="java.lang.String"/>
    <field name="SITE_NAME" class="java.lang.String"/>
    <field name="IN_TIME" class="java.lang.String"/>
    <field name="OUT_TIME" class="java.lang.String"/>
    <field name="CREATE_ON" class="java.lang.String"/>
    <title>
        <band height="50" splitType="Stretch">
            <staticText>
                <reportElement x="0" y="20" width="100" height="30" forecolor="#007BFF" uuid="46c45769-ac34-41af-bdb4-c45729f333b9"/>
                <textElement textAlignment="Center" verticalAlignment="Middle">
                    <font size="14" isBold="true"/>
                </textElement>
                <text><![CDATA[Call Logs]]></text>
            </staticText>
            <textField pattern="MMM d, yyyy h:mm:ss a">
                <reportElement x="450" y="20" width="330" height="30" forecolor="#007BFF" uuid="7a3c6c9d-bf5b-487d-8e13-720ecda75b61"/>
                <textElement textAlignment="Right" verticalAlignment="Middle">
                    <font size="12" isBold="true"/>
                </textElement>
                <textFieldExpression><![CDATA["Report Generated on:  "+new java.util.Date()]]></textFieldExpression>
            </textField>
        </band>
    </title>
    <detail>
        <band height="243" splitType="Stretch">
            <printWhenExpression><![CDATA[$V{REPORT_COUNT} != 0]]></printWhenExpression>
            <componentElement>
                <reportElement x="41" y="24" width="720" height="200" uuid="24f1ba3d-2de9-4a32-9f9f-75d592f91359">
                    <printWhenExpression><![CDATA[$V{REPORT_COUNT} == 1]]></printWhenExpression>
                </reportElement>
                <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="table" uuid="f76f4c9b-3345-4d0e-8cad-dd8a75913665">
                        <connectionExpression><![CDATA[$P{REPORT_CONNECTION}]]></connectionExpression>
                    </datasetRun>
                    <jr:column width="104" uuid="229daaed-80a6-42c5-9ade-2f0c75d1963e">
                        <jr:columnHeader style="Table_CH" height="30">
                            <staticText>
                                <reportElement x="0" y="0" width="104" height="30" uuid="cfa9503a-bbfe-41fb-96a6-46c633a691fc"/>
                                <text><![CDATA[NAME]]></text>
                            </staticText>
                        </jr:columnHeader>
                        <jr:detailCell style="Table_TD" height="30">
                            <textField>
                                <reportElement x="0" y="0" width="104" height="30" uuid="d0395260-510c-44dd-88c0-3c5ab97a6f65"/>
                                <textFieldExpression><![CDATA[$F{NAME}]]></textFieldExpression>
                            </textField>
                        </jr:detailCell>
                    </jr:column>
                    <jr:column width="101" uuid="94785e2c-8fa8-4402-8506-4f234bf064bd">
                        <jr:columnHeader style="Table_CH" height="30">
                            <staticText>
                                <reportElement x="0" y="0" width="101" height="30" uuid="03b5ef55-113a-485c-9d09-519dcc9d6152"/>
                                <text><![CDATA[MOBILE]]></text>
                            </staticText>
                        </jr:columnHeader>
                        <jr:detailCell style="Table_TD" height="30">
                            <textField>
                                <reportElement x="0" y="0" width="101" height="30" uuid="dba6ea8d-23d1-4af3-bf64-475a5266b85f"/>
                                <textFieldExpression><![CDATA[$F{MOBILE}]]></textFieldExpression>
                            </textField>
                        </jr:detailCell>
                    </jr:column>
                    <jr:column width="101" uuid="8151f7e4-e41b-430b-8730-b5499a6d5d1f">
                        <jr:columnHeader style="Table_CH" height="30">
                            <staticText>
                                <reportElement x="0" y="0" width="101" height="30" uuid="2321d1fa-4e3f-4d3f-8727-bae188bafd7b"/>
                                <text><![CDATA[COMPANY_NAME]]></text>
                            </staticText>
                        </jr:columnHeader>
                        <jr:detailCell style="Table_TD" height="30">
                            <textField>
                                <reportElement x="0" y="0" width="101" height="30" uuid="d9d2d45e-98cc-4f52-8b3d-4d2829d5df34"/>
                                <textFieldExpression><![CDATA[$F{COMPANY_NAME}]]></textFieldExpression>
                            </textField>
                        </jr:detailCell>
                    </jr:column>
                    <jr:column width="101" uuid="1c6cc322-6497-437a-8945-c1c3f08287b0">
                        <jr:columnHeader style="Table_CH" height="30">
                            <staticText>
                                <reportElement x="0" y="0" width="101" height="30" uuid="2720a693-f022-4498-93e3-4d0aa7b37982"/>
                                <text><![CDATA[SITE_NAME]]></text>
                            </staticText>
                        </jr:columnHeader>
                        <jr:detailCell style="Table_TD" height="30">
                            <textField>
                                <reportElement x="0" y="0" width="101" height="30" uuid="c34d16ae-244f-4b73-8e54-71655a5fc231"/>
                                <textFieldExpression><![CDATA[$F{SITE_NAME}]]></textFieldExpression>
                            </textField>
                        </jr:detailCell>
                    </jr:column>
                    <jr:column width="105" uuid="7b35eec3-38c2-4b76-aa58-d7cb1dc0925c">
                        <jr:columnHeader style="Table_CH" height="30">
                            <staticText>
                                <reportElement x="0" y="0" width="105" height="30" uuid="29e866cb-fecc-4854-abdb-4e131883b68e"/>
                                <text><![CDATA[IN_TIME]]></text>
                            </staticText>
                        </jr:columnHeader>
                        <jr:detailCell style="Table_TD" height="30">
                            <textField>
                                <reportElement x="0" y="0" width="105" height="30" uuid="d3b432d0-95e4-4d4a-b6a8-4984f3c95164"/>
                                <textFieldExpression><![CDATA[$F{IN_TIME}]]></textFieldExpression>
                            </textField>
                        </jr:detailCell>
                    </jr:column>
                    <jr:column width="104" uuid="ba443c16-d86f-483c-9e79-035cfc42ce40">
                        <jr:columnHeader style="Table_CH" height="30">
                            <staticText>
                                <reportElement x="0" y="0" width="104" height="30" uuid="a6d98a0b-d280-475a-8b7e-7e88778c5dcd"/>
                                <text><![CDATA[OUT_TIME]]></text>
                            </staticText>
                        </jr:columnHeader>
                        <jr:detailCell style="Table_TD" height="30">
                            <textField>
                                <reportElement x="0" y="0" width="104" height="30" uuid="9fa9cd8c-da08-4871-9cb1-6655d0a14e34"/>
                                <textFieldExpression><![CDATA[$F{OUT_TIME}]]></textFieldExpression>
                            </textField>
                        </jr:detailCell>
                    </jr:column>
                    <jr:column width="104" uuid="3aac9567-fd44-4954-8542-60806e43e960">
                        <jr:columnHeader style="Table_CH" height="30">
                            <staticText>
                                <reportElement x="0" y="0" width="104" height="30" uuid="81d7af3f-bfd5-4bda-8c4c-645d44c718b9"/>
                                <text><![CDATA[CREATE_ON]]></text>
                            </staticText>
                        </jr:columnHeader>
                        <jr:detailCell style="Table_TD" height="30">
                            <textField>
                                <reportElement x="0" y="0" width="104" height="30" uuid="502689db-da95-4dba-bdd9-fc066be71100"/>
                                <textFieldExpression><![CDATA[$F{CREATE_ON}]]></textFieldExpression>
                            </textField>
                        </jr:detailCell>
                    </jr:column>
                </jr:table>
            </componentElement>
        </band>
    </detail>
</jasperReport>

And this is my Java code:

public class PDFReportGenerator {
    public static String ACTUAL_REPORT_PATH = "E:\\path\\Blank_A4_Landscape.jrxml";
    public static String PDF_PATH = "E:\\path\\Blank_A4_Landscape.pdf";

    public static void main(String[] args) throws JRException, ClassNotFoundException, SQLException{
        generateReport();
    }

    static void generateReport() {
        Connection conn = null;
        ResultSet rs = null;
        JasperReport jasperReport = null;
        JasperPrint print = null;
        String query ="SELECT * FROM CODE";
        try {   
            if(conn==null) {
                try {
                    conn= OracleConnection.getConnection();
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }   
        CallableStatement cstmt = conn.prepareCall(query);
        rs=cstmt.executeQuery();
        JRResultSetDataSource resultSetDataSource = new JRResultSetDataSource(rs);      
        jasperReport = JasperCompileManager.compileReport(ACTUAL_REPORT_PATH);
        Map <String, Object> parameters = new HashMap <String, Object>();
        print = JasperFillManager.fillReport(jasperReport, parameters, resultSetDataSource);
        JasperExportManager.exportReportToPdfFile(print, PDF_PATH);
        } catch(Exception e) {
            System.out.println(e);}
    }
}

回答1:


Since your subDataset executes a query and you are passing a database connection to it.

<connectionExpression><![CDATA[$P{REPORT_CONNECTION}]]></connectionExpression>

In java you need to pass this connection to fillReport instead of your JRResultSetDataSource

print = JasperFillManager.fillReport(jasperReport,parameters, conn);

Another solution to keep the JRResultSetDataSource as main datasource would be to create another datasource for the subreport and pass this as a parameter modifying the datasetRun expression in jrxml accordingly, hence using this datasource instead of report connection.


To clarify, it works in IDE because in IDE you are selecting the connection before you run the report, <property name="com.jaspersoft.studio.data.defaultdataadapter" value="Oracle"/>, but these settings are not valid/loaded when running directly from java.



来源:https://stackoverflow.com/questions/58078369/why-is-my-subdataset-blank-when-i-run-report-from-java-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!