JasperReports 3.6: Creating a Report from Model Beans of Java Applications

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

A common data source is JavaBeans, which holds data at runtime. JasperReports allows you to generate reports directly from data contained in Java objects such as JavaBeans and Plain Old Java Objects (POJOs). This article by Bilal Siddiqui, author of JasperReports 3.6 Development Cookbook, shows you how to connect your iReport installation to your JavaBeans or POJOs, and then access the data contained in the beans to generate your report.

(For more resources on JasperReports, see here.)

Getting ready

You need a Java JAR file that contains class files for the JavaBeans required for this recipe. A custInvoices.jar file is contained in the source code (chap4). Unzip the source code file for this article and copy the Task5 folder from the unzipped source code to a location of your choice.

How to do it...

Let's start using Java objects as data storage units.

  1. Open the ModelBeansReport.jrxml file from the Task5 folder of the source code for this article (chapt 4). The Designer tab of iReport shows a report containing data in the Title, Column Header, Customer Group Header1 and Detail 1 sections, 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 in the following screenshot:

    JasperReports 3.6

  3. A new window named Connections / Datasources will open, as shown next. This window lists an Empty data source as well as the datasources you have made so far.

    JasperReports 3.6

  4. Click the New button 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. Select JavaBeans set datasource from the datasource types, as shown next. Click the Next button.

  6. A new window named JavaBeans set datasource will open, as shown in the following screenshot:

    JasperReports 3.6

  7. Enter CustomerInvoicesJavaBeans as the name of your new connection in the text box beside the Name field, as shown in the following screenshot:

    JasperReports 3.6

  8. Enter com.CustomerInvoicesFactory as the name of the factory class in the text box beside the Factory class field, as shown in the following screenshot:

    JasperReports 3.6

    This com.CustomerInvoicesFactory class provides iReport with access to JavaBeans that contain your data.

  9. Enter getBeanCollection as the name of the static method in the text box beside The static method... field, as shown in the following screenshot:

    JasperReports 3.6

  10. Leave the rest of the fields at their default values. Click the Test button to test your new connection to the JavaBeans datasource. You will see an Exception message dialog.
  11. This exception message occurs because iReport can't find your factory class. Dismiss the message box by clicking OK.
  12. Click the Save button at the bottom of the JavaBeans set datasource window and close the Connections / Datasources window as well.
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.)

  • Click Tools from the main iReport menu and select Options from the drop-down list, as shown in the following screenshot:

    JasperReports 3.6

  • A window named Options will open, as shown in the following screenshot:

    JasperReports 3.6

  • Click on the Classpath tab. You will see a default classpath in a path table, as shown below. Click the Add JAR button on the top-right part of the Classpath tab.

    JasperReports 3.6

  • A file browser will appear. Browse to the Task5 folder that you copied in the Getting ready section. Select the custInvoces.jar file and click the Open button, as shown in the following screenshot:

    JasperReports 3.6

  • The path to the JAR file containing JavaBeans will be added to the path table, as shown next. Click the OK button at the bottom of the Options window.

    JasperReports 3.6

  • Click on the Report Datasources icon to the right of the datasources drop-down list just below the main menu, as shown encircled in the following screenshot. A new window named Connections / Datasources will open, as shown next. It will show a list of available connections. Select the CustomerInvoicesJavaBean connection and click the Modify button.

    JasperReports 3.6

  • The JavaBeans set datasource window of step 6 will open. Click on the Test button at the bottom of the window. You will see a Connection test successful! message dialog. Click OK to dismiss it.
  • Click the Save button at the bottom of the JavaBeans set datasource window to save the connection settings.
  • You will see a Connections / Datasources window with the CustomerInvoicesJavaBean connection set as default. Click the close icon on the top-right corner of the window to close it.
  • Click the Report query button on the right of the Preview tab. A Report query dialog will appear, as shown in the following screenshot:

    JasperReports 3.6

  • Click on the JavaBeans Datasource tab. The JavaBeans Datasource tab will appear, as shown in the following screenshot:

    JasperReports 3.6

  • Enter com.CustomerInvoice as name of the class in the text box beside the Class name field. Then click the Read Attributes button. It will read the fields of the com.CustomerInvoice class and list them in the text area below the Class name field, as shown in the following screenshot:

    JasperReports 3.6

  • Select all the fields of the com.CustomerInvoice class except the class field, as shown in the in the following screenshot. Click the Add selected field(s) button at the bottom of the text area containing the fields list. This will show selected fields in the lower-half of the Report query dialog. Click the OK button.

    JasperReports 3.6

  • Switch to the Preview tab and you will see all the invoices listed in the report grouped by customer names.
  • How it works...

    In this article you have learned how to create a connection to JavaBeans and then use the data stored in the beans to generate your report. For this purpose, you did four things:

    1. You configured the name of a factory class in step 8 of the recipe.
    2. The factory class should have a static method to instantiate or access your JavaBeans. You configured the name of this static method in step 9 of the recipe.
    3. You provided a classpath to a JAR file, which contains the factory class as well as a class for the JavaBeans in step 16.
    4. Then you configured the properties of your JavaBeans as fields in iReport in steps 23 to 25. This enables iReport to fetch data from your JavaBeans.

    JasperReports internally calls the getBeanCollection() static method of the factory class. This method should contain the Java code that returns the actual JavaBeans from which you want to generate the report. For the sake of simplicity, I have returned a hardcoded object as shown in the following piece of code:

    public static Vector getBeanCollection() {
    CustomerInvoice invoice001 = new CustomerInvoice (
    1001,"Packt Publishing","Packing Material","Mar09",5020);
    CustomerInvoice invoice002 = new CustomerInvoice (
    1002,"Packt Publishing","Offset Paper","Apr09",3000);
    //Other instance creation calls to the CustomerInvoice class
    Vector invoices = new Vector();

    invoices.add(invoice001);
    invoices.add(invoice002);
    //more calls to invoices.add() method

    return invoices;
    }

    Summary

    With this we complete the four part article series which was focused 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.


    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


    Your rating: None Average: 4 (1 vote)

    Post new comment

    CAPTCHA
    This question is for testing whether you are a human visitor and to prevent automated spam submissions.
    U
    G
    3
    V
    e
    a
    Enter the code without spaces and pay attention to upper/lower case.
    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