Feeds:
Posts
Comments

Archive for the ‘JasperReport’ Category

In many different forums, I have seen posts regarding how to send parameters to JasperReport. If you are still looking for an answer, you can definitely find it over here.

I am assuming that you are familiar with JasperReport and the JRXML template.

JasperReport allows you to pass parameters to the report during runtime. In order to do so you need to:

 

  • Declare the parameter in your report template (jrxml) as follow
    <parameter name="itemName" class="java.lang.String"/>

    You can set a default value for the parameter in case it is not been passed. In order to do so, you can use the child element defaultValueExpression. 

  • You can then refer to the parameter that you have declared in your template using the syntax “$P{PARAMETER NAME}”
  • To refer to the parameter declared earlier in a text field use the follow syntax
    <textFieldExpression>
    $P{ itemName }
    </textFieldExpression>

    The text field will display the value of this parameter when the report is filled.

  • To pass the parameter declared earlier to a query, use the following syntax
    <queryString>
    <!&#91;CDATA&#91;
    SELECT * FROM product WHERE itemName = $P{ itemName } ORDER BY inventoryDate
    &#93;&#93;>
    </queryString>
  • To dynamically modify portions of the SQL query or to pass the entire SQL query as a parameter the syntax differs a little. BE CAREFUL. The ! character is needed. See the following example
    <queryString>
    <!&#91;CDATA&#91;
    SELECT * FROM $P!{MyTable} ORDER BY $P!{OrderByClause}
    &#93;&#93;>
    </queryString>

The parameters here are used to pass the table name in the FROM clause and the column names in the ORDER BY clause. Note that you cannot use the normal parameters (i.e. $P{}) to dynamically modify your query.

Please note that the reporting engine first deals with the $P!{} parameter references by using their values to obtain the final form of the SQL query, and only after that it transforms the rest of the $P{} parameters.

 

 Reference: The Definitive Guide to JasperReports by Teodor Danciu and Lucian Chirita

Advertisements

Read Full Post »