Use JasperReports to Create a PDF Report
Use the JasperReports reporting tool to include static data in J2EE applications and generate dynamic presentation reports.
by Deepak Vohra
April 25, 2007
JasperReports is an open source tool for Java reporting. You can use this tool to generate reports from static data, or data retrieved from a database, and export them as HTML, PDF, XLS, CSV or XML report files. The JasperReports tool lets you easily include these static reports in Java/J2EE applications to create dynamic reports for presentations.
In this article, you'll learn how to use the JasperReports tool in the WebLogic Server to generate a dynamic report. (If necessary, you can also use another application server such as JBoss.) In a JasperReports configuration file, you'll specify a JDBC connection with the MySQL database to retrieve data for the report. This report will be created as a PDF that opens in the Adobe Acrobat Reader.
You may not have the necessary tools to follow the processes outlined in this article. Here is a list of necessary software and some steps to complete before you can start generating reports (see Resources):
- Download the JasperReports tool (jasperreports-1.1.1.jar or a later version).
- Download the iText Java-PDF library (itext-1.1.jar or a later version).
- Download the Jakarta POI ZIP file (poi-bin-2.5.1-final-20040804.zip). Extract the file to a directory.
- Download the Jakarta CommonsDigester component (version 1.7 or later), Jakarta Commons BeanUtils component (version 1.1 or later), Jakarta Commons Collections component (version 2.0 or later) and Jakarta Commons Logging component (version 1.0 or later).
- Download the WebLogic Server 8.1. Install the server. (You can also use WebLogic Server 10 to configure a JDBC data source.)
- Download the MySQL 5.0 database.
- Download the MySQL JDBC driver Connector/J.
Once you've completed these steps, create an example database table, Catalog, from which you'll generate the report. The SQL script to create the table is shown in Listing 1.
Next, to create a JDBC connection to the MySQL database from the WebLogic Server, add the MySQL JDBC driver JAR file (mysql-connector-java-3.1.11-bin.jar) to the CLASSPATH variable of the startExamplesServer script in the directory:
Start the WebLogic Server, and then the administration console. In the administration console, select the Services>JDBC >Connection Pools node. Then click on the Configure a New JDBC Connection Pool link to create a new JDBC connection pool. Next, select the Database Type as MySQL and Database Driver as MySQL Driver Type 4 (com.mysql.jdbc.Driver). Click on the Continue button.
In the Connection Properties frame, specify Database Name as test, Host Name as localhost, Port as 3306 and user name as root. Click on the Continue button.
In the Test Database Connection frame, click on the Test Driver Configuration button, and a JDBC connection is configured with the MySQL database. Then, in the Configure a JDBC Connection Pool frame, click on the Create and Deploy buttons, and a JDBC connection pool is created.
Next, select the Services>JDBC>Data Sources node. Click on the Configure a New JDBC Data Source link, and specify the data source name and JNDI name (for example, MySQLDS). Click on the Continue button.
In the Connect to Connection Pool frame, select the JDBC connection pool that is configured with the MySQL database. Click on the Continue button.
If not already selected, choose the examplesServer option in the Target the Datasource frame. Click on the Create button, and a data source with the MySQL database is created.
If you don't already have it on your desktop, download and install Adobe Acrobat Reader (see Resources). Add the JAR files to the CLASSPATH variable in the startExamplesServer script (see Table 1). These files are required to generate a JasperReports report.
Configure the JasperReports XML File
A JasperReports report design is specified in an XML configuration file. The configuration file is based on the jasperreport.dtd file. The root element of the configuration file is called jasperReport. Other elements in a JasperReports XML file are listed with common sub-elements and attributes in Table 2.
Back to top