About Us
Contact us

Facebook profile


Creating Database Reports With JasperReports
Bookmark and Share


Our previous two articles on JasperReports (see Resources), have had reports displaying static text only. Although useful to demonstrate how to generate a report using JasperReports, those examples have little practical value. In practice, the vast majority of reports are created to display some kind of database data. In this article we will demonstrate how to generate a report from database data using JasperReports. Basic familiarity with JasperReports is assumed, see Resources for other introductory articles on JasperReports.

The Data

For this article, we will develop a sample report for a fictitious technology training institute. The report will contain the course number, course name and instructor for each course. The code will read from two database tables, the following SQL scripts were used to create the tables:

create table instructors(
  instructor_id int8 primary key,
  first_nm varchar,
  last_nm  varchar);


create table courses (
  course_id int8 primary key,
  course_cd varchar,
  course_nm varchar,
  instructor_id int8 
  REFERENCES instructors(instructor_id));

The Relational Database Management System used was PostgreSQL 8.0, but the scripts can be easily modified to be used by another RDBMS. The database will initially be populated with the following data:

mydb=# select * from instructors;
 instructor_id | first_nm |   last_nm
             1 | David    | Heffelfinger
             2 | John     | Doe
             3 | Alice    | Jones
             4 | Mary     | Wang
             5 | Pedro    | Gonzalez
(5 rows)


mydb=# select * from courses;
 course_id | course_cd |                course_nm                | instructor_id 
-----------+-----------+-----------------------------------------+-------------- -
         1 | JAVAEEI   | Introduction To Java EE                 |             1
         2 | JAVAEEA   | Advanced Java EE                        |             1
         3 | LINUXI    | Introduction To Linux                   |             2
         4 | LINUXA    | Advanced Linux                          |             3
         5 | JASPI     | Introduction to JasperReports           |             4
         6 | SWINGI    | Client Side Java Programming with Swing |             5
         7 | JAVAI     | Introduction to Java                    |             5
(7 rows)

Next - Creating The JRXML File