Java Reporting With Jasper Reports - Part 2

how to call jasper report from java

Welcome back to Java Reporting series. For those who didn't read the introductory article; have a look here before we get started. Today we're going to have a quick tour in JasperReport architecture, development lifecycle, report definition files, and finally we're going to set up our environment and start work in a sample application.


As shown in the above figure JasperReports architecture is based on declarative XML fileswhich by convention have an extension of jrxml that contains the report layout. A lot of third-party design tools were produced to generate your jrxml file in a smooth way (like iReport or JasperAssistant)Design file is supposed to be filled by report's result which is fetched from database, XML files, Java collection, Comma-separated values or Models. Jasper can communicate with those data-sources and more, it can merge any number of data-sources together and manipulates the results of any combinations. This communication goes through JDBC, JNDI, XQuery, EJBQL, Hibernate or existing Oracle PL/SQL. You also can define your own data-source class and pass it to jasper engine directly. After defining your report design layout in jrxml format and determining your data source(s) jasper engine does the rest of work. It compiles your design file and fills it with results fetched from data-source and generates your report to the chosen exporting format (PDF, Excel, HTML, XML, RTF, TXT …, etc.)

Report Definition file structure (jrxml):

Sample application

We're going to divide the work into two steps:

Generate the report design (jrxml file).

Implement application that assigns data source, compiles jrxml file and exports result in the chosen format.

Designing The Report

First we create new text file and rename it to sample_report.jrxml, this file should contain the following XML tags.

The above XML file consists of the following main sections that defining report behavior and layout:

    <queryString>: contains the SQL statement which retrieves the report result. <field name>: defines the resulted fields from the query, and give them name to reuse them into the report body [they are case-sensitive]. <staticText>: contains the header titles "Item Name" in <![CDATA[Item Name]]> tag format. <textFieldExpression>: defines the appearance of result field. $F: is a variable contains the value of Query result predefined field in the tag <field name>.

Once we finished the report design file, save it in C:\ directory.

Implementing The Report Service:

- Create a new java project.

- Import the jars listed in environment section to your project libraries.

- Create new class and import the following packages

- Create a connection to my data-source; initialize the report parameter into empty HashMap then compile our jrxml file into JasperReport object and finally fill the JasperPrint object by data from data-source connection.


Category: Bank

Similar articles: