FCI-Helwan blog

Just another FCI-H weblog

Java Reporting – Part 3

Through the previous articles (Part 1 and Part 2) we’ve covered the jasper introduction, architecture, environment, and finally we managed to run a sample report.

Today we’re going to work with visual report design tools and enhance our sample application to receive run-time parameters.

As we mentioned before JasperReports and other reporting tools were introduced to facilitate the reporting development cycle, but for end-developers working with declarative files –xml- is usually considered as two-sided coin. Although they are more flexible and popular, the overhead of designing report layout entirely by xml tags deserve reconsidering about using reporting tool.

Thanks to open-source community we don’t need to design our report layout through declarative files by ourselves anymore, a visual design tools were introduced to the open-source reporting engines. iReport and JasperAssistant are a good examples for JasperReports visual designer they had completed the missing block in constructing robust java reporting tools. As example of using design tools we’ll explore iReport and continue our tutorial application with it.

First you need to download the executable file from here, there are many versions you can download anyone –personally I use v2.0.4–. Install the executable file, start iReport and let’s work in the previous article sample report:

1- Open File menu -> New Document

2- After creating the empty report we need to set our data-source connection to test the report in the design time, to set the connection go to Data menu –> Connection/Data Sources, click on ‘New’ button, choose your datasource type from the list –in my case its ‘Database JDBC connection’-, and fill the connection parameters with your arguments.

3- Now we have an active connection to the data-source, next step is to define the report query we’ll use to fetch the report results. From Data menu –> choose Report Query, and then enter the report query as shown:

You can notice the fields marked by red circle, they are generated fields from your SQL query projection that will be used as report result.

4- Now let’s design our report layout, at the column header band we’ll add to the report header as a Static Text element from the tools –the one marked by red circle-, then at the details band we’ll add the dynamic data-source fields which are resulted from query projection, they can be added from the left panel ‘Document structure’ tree ‘Fields’ node –the one marked with blue circle-.

You may notice the three elements in the Document Structure panel: Parameters, Fields and Variables, those are the heart of the JaperReports they control every thing regarding the report behavior and results, lets clarify them in more detail:

  • Parameters: contain the arguments passed from the application code in run-time, we use it for building run-time search criteria and they are passed from code in the form of HashMap, written in xml by the following format: $P{REPORT_DATE}.
  • Fields: the dynamic result of the fields retrieved from data-source, they represent the report result, written in xml by the following format: $F{ITEM_NAME}.
  • Variables: they are two types, JasperReports built-in variables like (PAGE_NUMBER, PAGE_COUNT, COLUMN_COUNT… etc.), and there are the user defined variables which are used to perform more complicated operations on report results (calculations, summarizing, condition branching…etc.), they are written in xml by the following format: $V{COLUMN_NUMBER}.

5- Its time to test our report, iReport enables report designer to test his work on live data-source connection and different exporting format. From Build menu –> choose the exporting format you want, I’ll choose JRViewer Preview, then choose ‘Execute (with active connection)‘. You should get the report result in the JRViewer as shown:

That was the first part of our lesson today; at the next part we’ll modify our sample application to accept run-time search criteria, we will modify both the design file and the application code to be able to pass parameter contains the dynamic items amount limit we want search for, as shown in the following steps:

1- In the design file we’ll add a new parameter and append it in the query where segment, right click on the Parameter node in the ‘Document Structure’ panel and choose add’ –> ‘Parameter’, enter the parameter name, say “itemAmount” and choose type class (Integer), then update the query statement by replacing the where condition item_amount <= 100 with item_amount <= $P{itemAmount}.

2- At the code we need to update the parameter hashmap by adding an entry to it contains [key: parameter name, value: parameter value] as following:

// connection is the data source we used to fetch the data from
Connection connection = establishConnection();
// jasperParameter is a Hashmap contains the parameters
// passed from application to the jrxml layout

HashMap jasperParameter = new HashMap();

// put the search criteria you want
jasperParameter.put(“itemAmount “,50);

// jrxml compiling process
jasperReport = JasperCompileManager.compileReport

// filling report with data from data source

jasperPrint = JasperFillManager.fillReport(jasperReport,jasperParameter, connection);

Run the application and you’ll notice that results are filtered according to your search criteria.

Here we reach the end of our today’s article. Next article is supposed to be the last one in the series, we’ll cover the charts reports design & development in action.

Navigate the series: 1, 2, 3, 4


June 10, 2008 - Posted by | JAVA


  1. very good work,
    thank you

    but is IReport can plugeed into IDEs such as netBeans ??

    Comment by m-hewedy | June 13, 2008 | Reply

  2. @m-hewedy

    Yes, JasperSoft has released the first version of iReport for NetBeans on 18-02-2008.
    you can download it from:

    there is also a plug-in for Eclipse – although i know ur a netbeans fan :)-, u can get it from:

    Comment by HOssam Sadik | June 17, 2008 | Reply

  3. It is possible while defining a report query apply more than one report query at once?

    thx for reply

    Comment by Anonymous | August 20, 2008 | Reply

  4. if they share the same resulted fields then you can handle it using sql UNION or UNION ALL, if they contain different fields then using sub-reports will be more effective.

    feel free to ask about whatever you find mysterious.

    Comment by Hossam Sadik | August 20, 2008 | Reply

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: