René Nyffenegger's collection of things on the web
René Nyffenegger on Oracle - Most wanted - Feedback -
 

Jasper - Parameters

First example

A simple report that displays for records that take two parameters O1 and O2. The values of the four records depend on values of the parameters passed. This report is quite similar to this example.
<?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              ="Demonstrating Subreports" 
  pageWidth         =        "595" 
  pageHeight        =        "845"
  columnWidth       =        "595" 
  leftMargin        =          "0" 
  rightMargin       =          "0" 
  topMargin         =          "0" 
  bottomMargin      =          "0"
>

  <parameter name="O1" class="java.lang.Float" />
  <parameter name="O2" class="java.lang.Float" />

  <queryString>
    <![CDATA[
    
  --------------

    select $P!{O1} + $P!{O2} a,
           $P!{O1} - $P!{O2} b,
           $P!{O1} * $P!{O2} c,
           $P!{O1} / $P!{O2} d
      from 
           dual

  --------------
]]>
  </queryString>

  <field name="A" class="java.lang.Float"> <fieldDescription><![CDATA[]]></fieldDescription> </field>
  <field name="B" class="java.lang.Float"> <fieldDescription><![CDATA[]]></fieldDescription> </field>
  <field name="C" class="java.lang.Float"> <fieldDescription><![CDATA[]]></fieldDescription> </field>
  <field name="D" class="java.lang.Float"> <fieldDescription><![CDATA[]]></fieldDescription> </field>

  <title>        <band splitType="Stretch"/> </title>
  <pageHeader>   <band splitType="Stretch"/> </pageHeader>
  <columnHeader> <band splitType="Stretch"/> </columnHeader>

  <detail>
    <band height="52" splitType="Stretch">

      <textField> <reportElement x="0" y= "0" width="475" height="13"/> <textElement/> <textFieldExpression class="java.lang.String"><![CDATA[""+$P{O1} + " + " + $P{O2} + " = " + $F{A} ]]> </textFieldExpression> </textField>
      <textField> <reportElement x="0" y="13" width="475" height="13"/> <textElement/> <textFieldExpression class="java.lang.String"><![CDATA[""+$P{O1} + " - " + $P{O2} + " = " + $F{B} ]]> </textFieldExpression> </textField>
      <textField> <reportElement x="0" y="26" width="475" height="13"/> <textElement/> <textFieldExpression class="java.lang.String"><![CDATA[""+$P{O1} + " * " + $P{O2} + " = " + $F{C} ]]> </textFieldExpression> </textField>
      <textField> <reportElement x="0" y="39" width="475" height="13"/> <textElement/> <textFieldExpression class="java.lang.String"><![CDATA[""+$P{O1} + " / " + $P{O2} + " = " + $F{D} ]]> </textFieldExpression> </textField>

    </band>
  </detail>

  <columnFooter> <band splitType="Stretch"/> </columnFooter>
  <pageFooter>   <band splitType="Stretch"/> </pageFooter>
  <summary>      <band splitType="Stretch"/> </summary>
</jasperReport>

Second example: parameters with default values

Same as the previous example except that the parameters now have a defalt value. When executed in iReports, press enter without previously entering a value to see effect.
<?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              ="Demonstrating Subreports" 
  pageWidth         =        "595" 
  pageHeight        =        "845"
  columnWidth       =        "595" 
  leftMargin        =          "0" 
  rightMargin       =          "0" 
  topMargin         =          "0" 
  bottomMargin      =          "0"
>

  <parameter name="O1" class="java.lang.Float"><defaultValueExpression><![CDATA[new Float(4.2)]]></defaultValueExpression></parameter>
  <parameter name="O2" class="java.lang.Float"><defaultValueExpression><![CDATA[new Float(9.3)]]></defaultValueExpression></parameter>


  <queryString>
    <![CDATA[
    
  --------------

    select $P!{O1} + $P!{O2} a,
           $P!{O1} - $P!{O2} b,
           $P!{O1} * $P!{O2} c,
           $P!{O1} / $P!{O2} d
      from 
           dual

  --------------
]]>
  </queryString>

  <field name="A" class="java.lang.Float"> <fieldDescription><![CDATA[]]></fieldDescription> </field>
  <field name="B" class="java.lang.Float"> <fieldDescription><![CDATA[]]></fieldDescription> </field>
  <field name="C" class="java.lang.Float"> <fieldDescription><![CDATA[]]></fieldDescription> </field>
  <field name="D" class="java.lang.Float"> <fieldDescription><![CDATA[]]></fieldDescription> </field>

  <title>        <band splitType="Stretch"/> </title>
  <pageHeader>   <band splitType="Stretch"/> </pageHeader>
  <columnHeader> <band splitType="Stretch"/> </columnHeader>

  <detail>
    <band height="52" splitType="Stretch">

      <textField> <reportElement x="0" y= "0" width="475" height="13"/> <textElement/> <textFieldExpression class="java.lang.String"><![CDATA[""+$P{O1} + " + " + $P{O2} + " = " + $F{A} ]]> </textFieldExpression> </textField>
      <textField> <reportElement x="0" y="13" width="475" height="13"/> <textElement/> <textFieldExpression class="java.lang.String"><![CDATA[""+$P{O1} + " - " + $P{O2} + " = " + $F{B} ]]> </textFieldExpression> </textField>
      <textField> <reportElement x="0" y="26" width="475" height="13"/> <textElement/> <textFieldExpression class="java.lang.String"><![CDATA[""+$P{O1} + " * " + $P{O2} + " = " + $F{C} ]]> </textFieldExpression> </textField>
      <textField> <reportElement x="0" y="39" width="475" height="13"/> <textElement/> <textFieldExpression class="java.lang.String"><![CDATA[""+$P{O1} + " / " + $P{O2} + " = " + $F{D} ]]> </textFieldExpression> </textField>

    </band>
  </detail>

  <columnFooter> <band splitType="Stretch"/> </columnFooter>
  <pageFooter>   <band splitType="Stretch"/> </pageFooter>
  <summary>      <band splitType="Stretch"/> </summary>
</jasperReport>

More

See also this link on how to pass parameters to a subreport.