JasperReports 3.5 for Java developers
If you want to create easily understood, professional, and powerful reports from
disordered, scattered data using a free, open source Java class library, this book on
JasperReports is what you are looking for. JasperReports is the world’s most popular
embeddable Java open source reporting library, providing Java developers with the power
to create rich print and web reports easily.
JasperReports allows you to create better looking reports with formatting and grouping,
as well as adding graphical elements to your reports. You can also export your reports to
a range of different formats, including PDF and XML. Creating reports becomes easier
with the iReport Designer visual designing tool. To round things off, you can integrate
your reports with other Java frameworks, using Spring or Hibernate to get data for the
report, and Java Server Faces or Struts for presenting the report.
This book shows you how to get started and develop the skills to get the most from
JasperReports. The book has been fully updated to use JasperReports 3.5, the latest
version of JasperReports. The previously accepted techniques that have now been
deprecated have been replaced with their modern counterparts in this latest version.
All the examples in this book have been updated to use XML schemas for report
templates. Coverage of new datasources that JasperReports now supports has been
added to the book. Additionally, JasperReports can now export reports to even more
formats than before, and exporting reports to these new formats is covered in this new
edition of the book.
The book steers you through each point of report setup, to creating, designing,
formatting, and exporting reports with data from a wide range of datasources, and
integrating JasperReports with other Java frameworks.
What This Book Covers
Chapter 1, An Overview of JasperReports, introduces you to JasperReports and how it
came to be. It gives you an insight to JasperReports’ capabilities and features, and also an
overview of the steps involved in generating reports using JasperReports.
Chapter 2, Adding Reporting Capabilities to Java Applications, teaches you how to add
reporting capabilities to your Java applications. You will have your development and
execution environment set up to successfully add reporting capabilities to your Java
applications by the end of this chapter.
Chapter 3, Creating Your First Report, shows you how to create, compile, and preview
your first report in both JasperReports’ native format and web browser. It also briefs you
about the JRXML elements corresponding to different report sections.
Chapter 4, Creating Dynamic Reports from Databases, continues with report creation,
exploring how to create a report from the data obtained from a database. It also teaches
you to generate reports that are displayed in your web browser in the PDF format.
Chapter 5, Working with Other Datasources, uses datasources other than databases, such
as empty datasources, arrays or collections of Java objects, Maps, TableModels, XML,
CSV files, and custom datasources to create reports, enabling you to create your own
datasources as well.
Chapter 6, Report Layout and Design, gets you creating elaborate layouts, by controlling
report-wide layout properties and styles, dividing the report data into logical groups,
adding images, background text, and dynamic data to the reports, conditionally printing
the report data, and creating subreports.
Chapter 7, Adding Charts and Graphics to Reports, takes you to more appealing reports
by showing how to take advantage of JasperReports’ graphical features and create reports
with graphical data like geometric shapes, images, and 2D and 3D charts.
Chapter 8, Other JasperReports Features, discusses the JasperReports features that
lets you create elaborate reports, such as displaying report text in different languages,
executing Java code snippets using scriptlets, creating crosstab reports, running a
query with the results of a different query, adding anchors, hyperlinks, and bookmarks
to the reports.
Chapter 9, Exporting to Other Formats, demonstrates how to export reports to the
formats supported by JasperReports, such as PDF, RTF, ODT, Excel, HTML, CSV,
XML, and plain text and how to direct the exported reports to a browser.
Chapter 10, Graphical Report Design with iReport, helps you get your hands on a
graphical report designer called iReport, so that you can design reports graphically, and
also, using iReport’s graphical user interface.
Chapter 11, Integrating JasperReports with Other Frameworks, explains how to integrate
JasperReports with several popular web application frameworks and ORM tools, such as
Hibernate, JPA, Spring, JSF, and Struts.
Graphical Report Design with iReport
So far, we have been creating all our reports by writing JRXML templates by hand.
JasperSoft, the company behind JasperReports, offers a graphical report designer called
iReport. iReport allows us to design reports graphically by dragging report elements
into a report template and by using its graphical user interface to set report attributes.
iReport started as an independent project by Giulio Toffoli. JasperSoft recognized the
popularity of iReport and, in October 2005, hired Giulio Toffoli and made iReport
the official report designer for JasperSoft. Like JasperReports, iReport is also open
source. It is licensed under the GNU Public License (GPL).
In 2008, iReport was rewritten to take advantage of the NetBeans platform. It is
freely available both as a standalone product and as a plugin to the NetBeans IDE.
In this chapter, we will be covering the standalone version of iReport; however, the material is also applicable to the iReport NetBeans plugin.
By the end of this chapter, you will be able to:
- Obtain and set up iReport
- Quickly create database reports by taking advantage of iReport’s
- Design reports graphically with iReport
- Add multiple columns to a report
- Group report data
- Add images and charts to a report
iReport can be downloaded from its home page at http://jasperforge.org/
projects/ireport by clicking on the Download iReport image slightly above
the center of the page.
Once we click on the image, we are directed to an intermediate page where we can
either log in with our JasperForge account or go straight to the download page.
Either logging in or clicking on the No Thanks, Download Now button takes us
to the iReport download page.
The standalone iReport product is in the first row of the table on the page. To
download it, we simply click on the Download link in the last column. Other
downloads on the page are for older versions of JasperReports, iReport NetBeans
plugin, and other JasperSoft products.
iReport can be downloaded as a DMG file for Macintosh computers, as a Windows
installer for Windows PCs, as a source file, as a ZIP file, or as a gzipped TAR file.
To install iReport, simply follow the usual application installation method for
If you chose to download the ZIP or gzipped TAR file, simply extract it into any
directory. A subdirectory called something like iReport-nb-3.5.1 will be created.
(The exact name will depend on the version of iReport that was downloaded.) Inside
this directory, you will find a bin subdirectory containing an executable shell script
called ireport and a couple of Windows executables, ireport.exe and ireport_w.
exe. On Windows systems, either EXE file will start iReport.
The difference between the two Windows executables is that the ireport.exe will display a command-line window when iReport is executed, and ireport_w.exe won't. Both versions provide exactly the same functionality.
On Unix and Unix-like systems, such as Linux and Mac OS, iReport can be started
by executing the ireport shell script.
The following screenshot illustrates how iReport looks when it is opened for the
Setting up iReport
iReport can help us quickly generate database reports. To do so, we need to
provide it with the JDBC driver and connection information for our database.
iReport comes bundled with JDBC drivers for several open source relational
database systems, such as MySQL, PostgreSQL, HSQLDB, and others. If we want
to connect to a different database, we need to add the JDBC driver to iReport’s
CLASSPATH. This can be done by clicking on Tools | Options and then selecting
the Classpath tab.
To add the JDBC driver to the CLASSPATH, click on the Add JAR button, and then
navigate to the location of the JAR file containing the JDBC driver. Select the JAR file
and click on the OK button at the bottom of the window.
We won't actually add a JDBC driver, as we are using MySQL for our examples, which is one of the RDBMS systems supported out of the box by iReport. The information just provided is for the benefit of readers using an RDBMS system that is not supported out of the box.
Before we can create reports that use an RDBMS as a datasource, we need to create a
database connection. In order to do so, we need to click on the Report Datasources
icon in the toolbar:
After doing so, the Connections / Datasources configuration window should
To add the connection, we need to click on the New button, select Database JDBC
connection, and then click on the Next> button.
We then need to select the appropriate JDBC driver, fill in the connection
information, and click on the Save button.
Before saving the database connection properties, it is a good idea to click on the
Test button to make sure we can connect to the database. If we can, we should see
a pop-up window like the following:
After verifying that we can successfully connect to the database, we are ready to
create some database reports.