Java Reporting – Part 3
Join the DZone community and get the full member experience.
Join For FreeToday we're going to work with visual report design tools and enhance our sample application to receive run-time parameters.
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}.
// 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 ("C://sample_report.jrxml"); // filling report with data from data source jasperPrint = JasperFillManager.fillReport(jasperReport,jasperParameter, connection);
Opinions expressed by DZone contributors are their own.
Comments