I’ve made a previous article about setting up your environment to be a “JasperReport-ready”. You can click here to recall it.
And now, in this article, i will guide you to build your first JasperReport application. For this part, i will split this tutorial into 2 parts.
1. Let’s setup a dummy database. I’m using MySQL 5.0. Suppose we have a database named db_jasper
CREATE TABLE employees ( employee_id VARCHAR(6) NOT NULL PRIMARY KEY, employee_name VARCHAR(30), salary INT(6) ); INSERT INTO employees VALUES ('E0001','Hans Kristanto',5000000); INSERT INTO employees VALUES ('E0002','Conrad Alvin',3500000); INSERT INTO employees VALUES ('E0003','Luna Hu',4500000); INSERT INTO employees VALUES ('E0004','Jina Seo',3650000); INSERT INTO employees VALUES ('E0005','Ivan Lu',4510000); INSERT INTO employees VALUES ('E0006','Dennis Ewing',500000);
2. Start your iReport designer.
3. Create a new report template
Click on File -> New, then choose any template paper. For example, Blank A4 (in iReport 3.7.0). For iReport 3.7.0 below, just follow the wizard as usual.
NOTES : For a while, never use Report Wizard! You have to do it manually.
Give it name and directory to be saved. Click on finish.
4. After that, you will see window like this
If you to the report designer, there are 7 sections. I will explain it one by one :
- Title : is section for placing title document. Title document will only appear on the first page of document.
- Page Header : is section for placing page header, for example, page number, title, etc. This header will appear in every page of report but not in the first page.
- Column Header : is section for placing column header for detail title, for example, Employee Name, Salary. You will see the different in the real application.
- Details : is section where you place all data. Detail section will iterate as much as fetched data. Again, you will see how it works in the real application.
- Column Footer : similar with Column Header, but placed in the foot.
- Page Footer : similar with Page Header, but placed in the foot.
- Summary : is section where you put all summarize. For example, summary of total employees, total profits/loss, etc.
You have to understand these 7 sections first before we continue to the next step. Already understand? let’s go ahead!
Click on New button on the right side, then follow the wizard. My suggestion :
– use Database JDBC Connection.
– give it name afterward.
– use com.mysql.jdbc.Driver as the JDBC Driver.
– type your database name on JDBC URL e.g. : jdbc:mysql://localhost/db_jasper
– type your username and password.
After that, you will see the drop down next to the database icon will be set into our new datasource :
Everything is done for setting up. Let’s continue for the next core tutorial on the part 2.