JasperReports 3.6: Creating a Report from Relational Data

Exclusive offer: get 50% off this eBook here
JasperReports 3.6 Development Cookbook

JasperReports 3.6 Development Cookbook — Save 50%

Over 50 recipes to create next-generation reports using JasperReports

$29.99    $15.00
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

JasperReports 3.6 Development Cookbook Over 50 recipes to create next-generation reports using JasperReports
Published: July 2010
eBook Price: $29.99
Book Price: $49.99
See more
Select your format and quantity:

(For more resources on JasperReports, see here.)

  • Enter localhost as the name of the server hosting your database in the input box beside the Server Address field, as shown in the following screenshot:

    JasperReports 3.6

    Note that here I am assuming your PostgreSQL is installed on the same PC as you are running iReport. If PostgreSQL is installed over a network, you will enter the IP address of the machine hosting PostgreSQL instead of localhost.

  • Enter jasperdb5 as the name of the database instance created in the Getting ready section in the Database field, as shown in the following screenshot:

    JasperReports 3.6

    The name of the database instance will be different depending upon which database instance you are using. For this recipe you are using a database named jasperdb5 (which you created earlier in the Getting ready section). But when you are executing some other recipe, you will enter the name of the database instance that contains sample data for that particular recipe.

  • Click the Wizard button available to the right of the Database text box. This will update the JDBC URL field, as shown in the following screenshot:

    JasperReports 3.6

  • Provide postgres as the value of the Username and Password fields, as shown in the following screenshot:

    JasperReports 3.6

  • Check the Save password checkbox beside the Password field shown in step 12 to save your database password permanently for reports generated using iReport.
  • Click the Test button to test the new database connection. In the case of a successful connection, you will see a Connection test successful message in a dialog window. Dismiss the message by clicking the OK button.
  • Click the Save button to save the newly created connection. You will see that the Connections / Datasources window will open showing your new connection set as the default connection in the connections list, as shown in the following screenshot:

    JasperReports 3.6

  • Now you are all set to generate a report from the data contained in your PostgreSQL installation. Whenever you are opening or viewing a report based on data contained in PostgreSQL, just make sure that PG is selected in the datasources drop-down list, as shown in the following screenshot:

    JasperReports 3.6

  • There's more...

    You have learned how to connect iReport to PostgreSQL. If you have some other database, you just need to select the appropriate JDBC driver from the JDBC Driver drop-down list in step 8 of the recipe.

    iReport comes bundled with drivers for the following open source databases:

    • MySQL
    • PostgreSQL
    • HSQLDB

    If you are not using an open source database, you will need to separately download and install the relevant JDBC drivers.

    summary

    This article showed how you can connect JasperReports to your relational database.


    Further resources on this subject:


    JasperReports 3.6 Development Cookbook Over 50 recipes to create next-generation reports using JasperReports
    Published: July 2010
    eBook Price: $29.99
    Book Price: $49.99
    See more
    Select your format and quantity:

    About the Author :


    Bilal Siddiqui

    Bilal Siddiqui is an Electronics Engineer, an XML consultant, and the founder of XML4Java.com, a company focused on simplifying e-business. After graduating in 1995 with a degree in electronics engineering from the University of Engineering and Technology Lahore, he began designing software solutions for industrial control systems. Later, he turned to XML and used his programming experience in C++ to build web- and WAP-based XML processing tools, server-side parsing solutions, and service applications. Bilal is a technology evangelist and a frequently published technical author.

    Bilal has been focusing exclusively on Java and XML-based open source tools and solutions since 2006. He has extensively used popular open source products such as JasperReports, ADempiere, Openbravo, and Eclipse. Bilal is a strong advocate of open source tools and is engaged not only in designing solutions based on open source tools but also collaborating with local universities in Lahore to train software and IT personnel in using open source technologies.

    Books From Packt


    iReport 3.7
    iReport 3.7

    Pentaho Reporting 3.5 for Java Developers
    Pentaho Reporting 3.5 for Java Developers

    Amazon SimpleDB Developer Guide
    Amazon SimpleDB Developer Guide

    RESTful Java Web Services
    RESTful Java Web Services

    Apache JMeter
    Apache JMeter

    Apache Geronimo 2.1: Quick Reference
    Apache Geronimo 2.1: Quick Reference

    Tomcat 6 Developer's Guide
    Tomcat 6 Developer's Guide

    JavaFX 1.2 Application Development Cookbook
    JavaFX 1.2 Application Development Cookbook


    Code Download and Errata
    Packt Anytime, Anywhere
    Register Books
    Print Upgrades
    eBook Downloads
    Video Support
    Contact Us
    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