Search code examples
mysqljasper-reports

How can I generate same row multiple times?


I am trying to make a sticker using jasper report.

below image shows the structure. enter image description here

I am passing a query to generate jasper report. I have design the above structure in 'detail field' of jasper report so it can't repeat ,now I need to generate my query-result multiple times.

which means if currently my query gives me below result:-

enter image description here

I wish to produce the same result n times.

So If my query repeats the same row n times then it will automatically generate this structure 4 or n times

So anybody knows how to repeat the same row in mysql result query or anybody have better solution than this to do this job.

my expected result is as below enter image description here


Solution

  • Modifying query is one solution example by using union

    SELECT REPEAT('a',1) UNION SELECT REPEAT('b',10); 
    

    but I will give you a pure jasper-reports solution, defining a parameter that indicates how many times your print should be repeated

    The pure jasper reports solution is using a subreport, passing an JREmptyDatasource(nrOfPrints) and the fields as parameters. The subreport will repeat the detail band as many time as the nrOfPrints and you can output the parameters (your main report fields) in it.

    Example

    Main report

    The parameter that defines how many times to repeat is RepeatNumber also see how I pass the fields as parameter to the subreport, you need to pass all your fields.

    <?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="RepatDataSource" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="ceca1b98-d43c-4ee0-8339-661aa2ea53a9">
        <parameter name="SUBREPORT_DIR" class="java.lang.String" isForPrompting="false">
            <defaultValueExpression><![CDATA["C:\\jdd\\projects\\StackTrace\\jasper\\"]]></defaultValueExpression>
        </parameter>
        <parameter name="RepeatNumber" class="java.lang.Integer">
            <defaultValueExpression><![CDATA[3]]></defaultValueExpression>
        </parameter>
        <queryString>
            <![CDATA[Your query]]>
        </queryString>
        <field name="field1" class="java.lang.String">
            <fieldDescription><![CDATA[]]></fieldDescription>
        </field>
        <detail>
            <band height="100" splitType="Stretch">
                <subreport>
                    <reportElement x="0" y="0" width="555" height="100" uuid="9d56da00-c1c9-4b2b-94e2-4019e4f58c8f"/>
                    <subreportParameter name="NR_REPEAT">
                        <subreportParameterExpression><![CDATA[$P{RepeatNumber}]]></subreportParameterExpression>
                    </subreportParameter>
                    <subreportParameter name="field1">
                        <subreportParameterExpression><![CDATA[$F{field1}]]></subreportParameterExpression>
                    </subreportParameter>
                    <dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.JREmptyDataSource($P{RepeatNumber}.intValue())]]></dataSourceExpression>
                    <subreportExpression><![CDATA[$P{SUBREPORT_DIR} + "RepatDataSource_subreport.jasper"]]></subreportExpression>
                </subreport>
            </band>
        </detail>
    </jasperReport>
    

    Subreport

    <?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="RepatDataSource_subreport" pageWidth="555" pageHeight="802" columnWidth="555" leftMargin="0" rightMargin="0" topMargin="0" bottomMargin="0" uuid="98ddead4-e116-4c91-9ecb-416c10c3065c">
        <parameter name="NR_REPEAT" class="java.lang.Integer"/>
        <parameter name="field1" class="java.lang.String" isForPrompting="false"/>
        <detail>
            <band height="108" splitType="Stretch">
                <textField>
                    <reportElement x="328" y="1" width="100" height="20" uuid="c5642fd7-9f63-4aa5-8503-16b1388c156b"/>
                    <textElement verticalAlignment="Middle"/>
                    <textFieldExpression><![CDATA[$V{REPORT_COUNT} + "/" +$P{NR_REPEAT}]]></textFieldExpression>
                </textField>
                <textField>
                    <reportElement x="45" y="35" width="125" height="20" uuid="5c2bb49a-ba95-4cb7-8c46-c32a0769e5e9"/>
                    <textElement verticalAlignment="Middle"/>
                    <textFieldExpression><![CDATA[$P{field1}]]></textFieldExpression>
                </textField>
                <line>
                    <reportElement x="0" y="0" width="555" height="1" uuid="dd3e7e6c-979e-421b-9f71-479e64c8023b"/>
                </line>
                <staticText>
                    <reportElement x="0" y="35" width="45" height="20" uuid="0443e2f4-25bd-4837-9c95-bca2b26b3996"/>
                    <textElement verticalAlignment="Middle"/>
                    <text><![CDATA[Name]]></text>
                </staticText>
                <staticText>
                    <reportElement x="214" y="1" width="114" height="20" uuid="0e20ec29-0092-41a3-b977-f8f64ff842ea"/>
                    <textElement verticalAlignment="Middle"/>
                    <text><![CDATA[Print Educational Books]]></text>
                </staticText>
            </band>
        </detail>
    </jasperReport>
    

    Output (with 1 record from my database)

    Result