logo
header art

Creating Reports with iReports and JasperReports

June 23, 2005

Creating a New Report

Create a Database Connection

To create a new data source to use in your report select "Datasource" from the iReport menu bar, then "Connections / Datasources". Click the "New" button and select "Database JDBC connection" from the first dropdown list. If connecting to a MySQL database, select the com.mysql.jdbc.Driver JDBC Driver. After completing the connection parameters, you should have a JDBC URL similar in form to: jdbc:mysql://192.168.1.100/NameOfYourDB.

If when you test your connection you receive an error dialog with the following message, "Communication link failure: java.io.IOException, underlying cause: Unexpected end of input stream", you should update your MySQL JDBC driver. The latest driver can be found at: http://www.mysql.com/products/connector/j/.

The MySQL driver that ships with iReport does not seem compatible with version 4.1 of MySQL.

At the time of this writing, mysql-connector-java-3.1.8a.zip was the latest production-ready archive available for download. Inside this Zip file, you will find a file named mysql-connector-java-3.1.8-bin.jar; copy this file into the iReport/lib directory, e.g., C:\Program Files\iReport-0.4.1\lib\, and delete any older versions, such as mysql-connector-java-3.0.8-stable-bin.jar.

When iReport starts, it will automatically load all .jar files found in the \lib directory. Restart iReports and test your new Datasource connection.

Creating a New Report File

To create a new report select "File / New Document" from the menu bar. Name the report and select your desired paper size. Do not use spaces in the report name. Next, you will need to set the active database connection in order to use values from your database queries. To do this, select "Build / Set active connection" from the menu and choose the connection defined earlier.

Add Fields From Database Table

First, define a query. Select "Datasource / Report query" from the menu bar; this will open the query dialog where we will create a select query to return values to our report. Enter the following: SELECT ID, Name, Address, City, PostalCode FROM Customer. Note: Explicitly select columns, otherwise only non-null columns will appear in the Report Fields selection dialog.

As you type this query, iReport will attempt to execute the query as you type. Note:  You may see exception messages while typing, this is normal behavior and is a way to let you know when the query is malformed. When a parsable query has been entered, iReport will display a list of returned columns beneath the query edit window.

Query Window

To add database fields to the report, select "View / Report Fields". This will open a dialog with a list of fields returned by the SQL query.

Query Window

Drag from the Field name grid column into the "detail" section of the report canvas. You should now have something like this:

Query Window

To test the report, select "Build / Execute report (using active conn.)" from the menu bar. The status window in iReport should display something similar to:

Compiling to file... JasperReports\Simple.jasper -> ...\TestReport.java 
Compilation running time : 300 
Filling report...  jrviewerExporting
Viewing with JasperReports Viewer
Running time : 600