How to use two different connections to Databases in iReport

This article shows how to use two different connections to two Databases in the one report in iReport editor by using a connection expression.

In one of the chapters in the iReport Ultimate Guide it is mentioned that it is possible to use the connection expression to point to the subreport to use different database then in Main report (iReport Ultimate Guide/Chapter 10.1.3 Specifying the Data Source).

But how? I have not found any samples related with this topic and desided to check this feature on a sample report. I managed it to work and now I want to share my results with you.

Here is the step-by-step description of my sample report and the example of the Connection Expression that I used in it:

In the iReport i set up two different data source connections to Foodmart sample database.

I called one of them foodmart(Data Source that was used in Main Report) and the other foodmart_sub_query(Data Source that was used in the Sub Report).

I created the Main report that used the "foodmart" connection to our sample database Foodmart
In the Main report I used simple Query that is shown below:

SELECT employee."employee_id" AS employee_employee_id,
       employee."first_name" AS employee_first_name
     FROM
       "public"."employee" employee
     WHERE 
       employee."employee_id" = $P{parameter1}

I created the SubReport that used foodmart_sub_query as Data Source.

I did this by setting up the properties that are shown below:

Set the Connection type of subreport to : Use a connection expresstion.

Set the Connection expression in subreport to:

com.jaspersoft.ireport.designer.utils.Misc.getConnection("foodmart_sub_query")

In the Sub Report I used the simple Query:

SELECT employee."employee_id" AS employee_employee_id,
       employee."first_name" AS employee_first_name,
       employee."last_name" AS employee_last_name
    FROM
       "public"."employee" employee
    WHERE employee."employee_id" = $P{parameter1}

I set the parameter1 in Sub Report equal to $F{employee_employee_id}. This report and subreport use two different connections when they are requesting the data. Thats all.

Comments

What should be the property name for studio?

Because this property is applicable to ireport oly

Feedback
randomness