Java Reporting – Part 3

DZone 's Guide to

Java Reporting – Part 3

· Java Zone ·
Free Resource
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 dynamic search criteria.
Here we reach the end of today's article. Next article is supposed to be the last one in the series, it will cover the charts reports design & development in action.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}