JasperReports 3.6: Creating a Report from Relational Data

by Bilal Siddiqui | June 2010 | Cookbooks Java Open Source

This is the first part of a four-part article series which focuses on advanced things such as working with a variety of data sources: relational data, XML data, model beans of Java applications, and also multiple relational databases at once. iReport will need to access these data sources in order to generate a report. iReport uses the concept of loose coupling between data sources and report design. Loose coupling means you can design your reports independent of the type of data source used. This means the same report design can work with any data source.

Relational databases are perhaps the most popular data sources used to hold application data. This article by Bilal Siddiqui, author of JasperReports 3.6 Development Cookbook, shows how you will connect JasperReports to your database. An open source database named PostgreSQL to is used to hold the sample data. That's why in this recipe you will connect iReport with your PostgreSQL installation. This article also shows that you can connect iReport to any of the popular databases in a similar manner.

(For more resources on JasperReports, see here.)

Getting ready

You will need PostgreSQL to follow this recipe. Refer to the installPostgreSQL.txt file included in the source code download (chap4), which shows how you will install and run PostgreSQL. Note that your installation of PostgreSQL should be up and running before you proceed.

The source code for this article also includes a file named CreateDbIntoPGS.txt, which will help you to create a database named jasperdb5.

How to do it...

The following simple steps will show you how to connect iReport to a database:

  1. Run iReport; it will open with a Welcome Window, as shown in the following screenshot:

    JasperReports 3.6

  2. If you have not made any database connection so far in your iReport installation, you will see an Empty datasource shown selected in a drop-down list just below the main menu. Click on the Report Datasources icon shown encircled to the right of the drop-down list, as shown in the following screenshot:

    JasperReports 3.6

  3. A new window named Connections / Datasources will open, as shown in the following screenshot. This window lists an Empty datasource as well as the datasources you have made so far.

    JasperReports 3.6

  4. Click the New button shown at the top right of the Connections / Datasources window. This will open a new Datasource selection window, as shown in the following screenshot:

    JasperReports 3.6

  5. You will see Database JDBC connection is selected by default. Click the Next button at the bottom of the Datasource window.
  6. A new window named Database JDBC connection will open, as shown in the following screenshot:

    JasperReports 3.6

  7. Enter PG as the name for your new database connection in the input box beside the Name field.

    PG is just a name for the database connection you are creating. You can give any name and create any number of database connections.

  8. Click on the JDBC Driver drop-down list; it will drop-down to show a list of available JDBC drivers. As you are connecting to the PostgreSQL database, select the Postgre SQL (org.postgresql.Driver) option from the drop-down list, as shown in the following screenshot:

    JasperReports 3.6

Sign up for a Packt account to see the rest of this article

Now that you've read a few articles, you might want to consider signing up for a Packt account. It takes a matter of seconds, will give you access to all the articles on PacktPub.com, and once you've signed up you'll be returned here to carry on reading your article.

Furthermore, you'll gain access to nine free ebooks, and be offered a free trial of PacktLib, Packt's online library. Simply enter your details here, or log in to your existing account.

Log in

...or register

Post new comment

Awards Voting Nominations Previous Winners
Judges Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software
Resources
Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software
Sort A-Z