Contents

 

Jasper Reports - Using Parameters

TIP: Dependency

Remember that parameters are evaluated in the order that you see in the list.

Therefore any parameters that you use that depend on another parameter have to be later in the list.

So I might have a parameter called COMPANY_ACCOUNT which is passed in to the report and accessed using $P{COMPANY_ACCOUNT} syntax (except for the where clause).

Then I would convert that numeric (Integer in this case) to a string based on the content such as:

Parameter COMPANY_ACCOUNT_STR =
($P{COMPANY_ACCOUNT}== null ? "**NA**" : "S-" + $P{COMPANY_ACCOUNT} )

In the list the correct order would be:

COMPANY_ACCOUNT
COMPANY_ACCOUNT_STR

If they are reversed then you might end up that COMPANY_ACCOUNT is not yet defined so the result COMPANY_ACCOUNT_STR would be **NA** (or perhaps null depending on another setting when data values missing).


TIP: Parameter usage in the SQL WHERE clause

Example: WHERE companies.name LIKE $P!{COMPANY_NAME}

It is important not to forget the exclamation point after the "$P" otherwise the Jasper Reports compiler will not translate the parameter value properly.

The where clause itself can be a parameter that you create dynamically at the start of the report using other paramters passed into the report.

Example: WHERE $P!{WHERE_CLAUSE}

The 'WHERE_CLAUSE' is a parameter (ensure it is the LAST parameter in the parameter list so it uses other parameters defined before this one).

Here is a sample of the WHERE_CLAUSE:

'WHERE companies.name LIKE ' + $P{COMPANY_NAME} ' AND ' companies.status LIKE ' + $P{COMPANY_STATUS}

The parameters COMPANY_NAME and COMPANY_STATUS are String type parameters passed into the report.

Always define a default value for parameters in case that parameter is not passed into the report (or is null).

 


TIP: The entire SQL query can be wrapped in a parameter.

This means that you can use another program to verify the query and then copy and paste into the database sql query string. This may save you some time if you do not like the iReport designer way of creating the query.