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.
Field | Type | Length |
---|---|---|
id | int | 4 |
name | varchar | 50 |
phone | varchar | 20 |
handphone | varchar | 20 |
address | varchar | 50 |
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.
PreparationThe 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.
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.
No comments:
Post a Comment