DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
The Latest "Software Integration: The Intersection of APIs, Microservices, and Cloud-Based Systems" Trend Report
Get the report
  1. DZone
  2. Data Engineering
  3. Databases
  4. Java Reporting – Part 3

Java Reporting – Part 3

Hossam Sadik user avatar by
Hossam Sadik
·
Jun. 17, 08 · Interview
Like (0)
Save
Tweet
Share
19.63K Views

Join the DZone community and get the full member experience.

Join For Free
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
("C://sample_report.jrxml");

// 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.
Database Java (programming language) Open source application Design

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • 11 Observability Tools You Should Know
  • Integrate AWS Secrets Manager in Spring Boot Application
  • 5 Steps for Getting Started in Deep Learning
  • Asynchronous Messaging Service

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: