0
votes

I'm trying to generate a Jasper report by passing parameters. The program runs and complates without error but no file is created. Also it works fine when used on reports without parameters.

Program :

Class.forName("com.mysql.jdbc.Driver");
Connection conn = null;
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test_reports","root", "root");
System.out.println("Loading Report Designs");
InputStream input = new FileInputStream(new File("parameterized_report.jrxml"));
JasperDesign jasperDesign = JRXmlLoader.load(input);

System.out.println("Compiling Report Designs");
JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);

System.out.println("Creating JasperPrint Object");
Map parameters = new HashMap();
parameters.put("yearParam","2010");

JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport,parameters,conn);

//Exporting the report
OutputStream output = new FileOutputStream(new File("Final.pdf"));

JasperExportManager.exportReportToPdfStream(jasperPrint, output);

System.out.println("Report Generation Complete");
conn.close();

Jasper report :

<?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="parameterized_report" language="groovy" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="37387775-86c0-41fc-ba9c-de01d6b29053">
    <property name="ireport.zoom" value="1.0"/>
    <property name="ireport.x" value="0"/>
    <property name="ireport.y" value="0"/>
    <parameter name="yearParam" class="java.lang.String">
        <parameterDescription><![CDATA[]]></parameterDescription>
        <defaultValueExpression><![CDATA[$F{year}]]></defaultValueExpression>
    </parameter>
    <queryString language="SQL">
        <![CDATA[select * from yearly_spending where year=$P{yearParam}]]>
    </queryString>
    <field name="company" class="java.lang.String">
        <fieldDescription><![CDATA[]]></fieldDescription>
    </field>
    <field name="year" class="java.lang.Integer">
        <fieldDescription><![CDATA[]]></fieldDescription>
    </field>
    <field name="spending" class="java.lang.Integer">
        <fieldDescription><![CDATA[]]></fieldDescription>
    </field>
    <background>
        <band splitType="Stretch"/>
    </background>
    <title>
        <band height="79" splitType="Stretch"/>
    </title>
    <columnHeader>
        <band height="61" splitType="Stretch">
            <staticText>
                <reportElement x="92" y="41" width="100" height="20" uuid="aad73e02-6885-4f31-a63a-7726e85e2bdb"/>
                <text><![CDATA[COMPANY ]]></text>
            </staticText>
            <staticText>
                <reportElement x="203" y="41" width="100" height="20" uuid="3d81629b-faa4-4e0e-8db2-7f21732e0a94"/>
                <text><![CDATA[YEAR]]></text>
            </staticText>
            <staticText>
                <reportElement x="319" y="41" width="100" height="20" uuid="26d9ea75-79f1-4da0-a7aa-e37c18422de3"/>
                <text><![CDATA[SPENDING]]></text>
            </staticText>
        </band>
    </columnHeader>
    <detail>
        <band height="23" splitType="Stretch">
            <textField>
                <reportElement x="92" y="0" width="100" height="20" uuid="dd81332b-d8d5-43ce-9d79-d4ce0b26df0d"/>
                <textFieldExpression><![CDATA[$F{company}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="203" y="0" width="100" height="20" uuid="68c786ad-49cb-44ea-ad20-463c498fe54d"/>
                <textFieldExpression><![CDATA[$F{year}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="319" y="0" width="100" height="20" uuid="5d3469e8-009d-43b0-8ecf-f89461706b7a"/>
                <textFieldExpression><![CDATA[$F{company}]]></textFieldExpression>
            </textField>
        </band>
    </detail>
    <summary>
        <band height="190" splitType="Stretch">
            <barChart>
                <chart>
                    <reportElement x="82" y="35" width="346" height="147" uuid="a481305f-8ab2-4944-85a8-9ec810d6eda6"/>
                    <chartTitle/>
                    <chartSubtitle/>
                    <chartLegend/>
                </chart>
                <categoryDataset>
                    <categorySeries>
                        <seriesExpression><![CDATA[$F{company}]]></seriesExpression>
                        <categoryExpression><![CDATA[$F{year}]]></categoryExpression>
                        <valueExpression><![CDATA[$F{spending}]]></valueExpression>
                    </categorySeries>
                </categoryDataset>
                <barPlot>
                    <plot/>
                    <itemLabel/>
                    <categoryAxisFormat>
                        <axisFormat/>
                    </categoryAxisFormat>
                    <valueAxisFormat>
                        <axisFormat/>
                    </valueAxisFormat>
                </barPlot>
            </barChart>
        </band>
    </summary>
</jasperReport>
1

1 Answers

0
votes

First thing year column is integer in the query read fields so you should change the parameter to integer and give any year like 2013 as default value

Second thing you can change the query to :-

     select * 
     from yearly_spending 
     where (year=$P{yearParam} or $P{yearParam} is null)

this query will give you all the results without selecting any parameter.