Monday, February 4, 2008

iReport Tutorial (part 2) - Creating Your First Report

Designing The Database and Table

After the iReport installation is completed, you can create your first report. Usually, a report need a data source, so we need a database. In this tutorial, I use MySQL database as the database. You must have MySQL installed on your computer to try this tutorial. I also recommend you learn the basic of MySQL first if you never use MySQL before.

First, you have to create a new database and name it contact. Inside it, create the new table named contact with a structure like below.

FieldTypeLength
idint4
namevarchar50
phonevarchar20
handphonevarchar20
addressvarchar50

After that, insert some records to the table. You can store your friends' name, address, and also their phone and cell phone number to the table.

Preparation

The next steps are the preparation to design the report. We need to set the classpath and create the connection to MySQL. So, follow the instructions below.

1. Setting Classpath

We need to set the classpath which stores the driver for MySQL. Click on the Option menu and then click Classpath. After that click Add JAR on the dialog box, then choose the MySQL driver. You will find it inside the lib folder in your iReport installation directory and the file name is mysql-connector-java-3.1.11-bin.jar. The file name can be different from yours depend on its version. Then, click Save Classpath to save the configuration.

2. Setting Up Database Connection

To create a database connection, click Data menu, and then click New to create new connection. After that you have to determine the data source type. Choose Database JDBC Connection. Then click Next so a new dialog box will appear. Fill the text field just like the picture below, you have to specify Connection Name, JDBC Driver, and JDBC URL. After that, enter the MySQL username and password according to your MySQL configuration and then click Test to test your configuration. If the connection test is success, click Save to save your configuration.

Designing The Report

To begin designing the new report just click File, than click New. Now you will face a window which provide a page to design the report's layout. But before you start to design, you have to make a query to retrieve the fields from the table. Click on Data menu then choose Report Query. Type SELECT * FROM contact in the text area. After that iReport will automatically retrieve the fields from the database according to your SQL command. Then click OK. For clearer information, look at the picture below.

After all required fields retrieved, you can start to design the report's layout by dragging the fields from the Document Structure panel to the designer page. For example you can see the report design at the picture below.

Run It!

The last step is execute your report. Before you execute it, click on Option then click Settings and click Compiler tab. Give a checkmark to the Use Reports Directory to Compile option and uncheck the keep .java file (if available) option. Click Save to save your changes.

Now, you are ready to execute your report, but don't forget to save it first. Just click Build, then click Execute (with active connection) to execute it.

iReport Tutorial (part 1) - Installing iReport Designer

iReport is a report designer tool for Java. You can download iReport Designer freely from sourceforge.net/projects/ireport. Before you install iReport (on Windows or Linux), The Java Development Kit (JDK) must be installed properly on your computer. Be sure that the JDK version that you are using is at least 1.5. Unless you can download the newer version of JDK from java.sun.com. Just for information, in this tutorial I used JDK 1.6 and iReport 2.0.4.

To install iReport Designer you just need to extract the compressed iReport binary file to a specified folder (i.e C:\Program Files if you are using Windows or /usr/local if you are using Linux). When you're done, you will have a new folder named iReport-2.0.4. You folder name can be different from mine depend on the iReport version that you used. After that, you have to enter that new folder and double-click on iReport.bat file if you are using windows. But, if you are using Linux, you can double-click iReport.sh file. Or you can try another way by typing ./iReport.sh on your Linux console (and don't forget that your current directory must be the iReport installation directory). If your iReport starts properly that means you have successfully installed iReport on your computer.