Jasper Reports and Data Sources

March 10, 2011

Tools & IDEs

«»

Introduction

Jasper Report is a powerful open source reporting architecture. It is completely written in java and is full of features required for reporting. Following are some of the key features supported.

  • Pixel-perfect and page-oriented reports for printing or continuous output for web or print
  • Dashboards, tables, crosstabs, charts and gauges
  • Report output in PDF, XML, HTML, CSV, XLS, RTF, TXT
  • Sub-reports to easily handle highly complex layouts
  • No limit to report size
  • Conditional printing
  • Internationalized and Localizable
  • Visual text rotation
  • Multiple data sources support

In this article the focus will be on the last point; i.e. the Support Jasper Reporting architecture provides for accessing data from various kinds of data sources. We will look into how a developer can use the powerful API support that jasper provides to handle the following types of data sources for creating the reports.

  • Database JDBC connection
  • XML file data source

What is a Data source!?

Before getting into the technical details of how jasper handles different data sources lets first understand what a data source is.
Any report; which is an intelligent representation of a result of search/study; will need to show Data[obviously!]

But where does this data come from?

It can either come from the User input or from Data base.

If the report is to be generated purely from what the user has entered and not from the data present in database; then there is NO requirement of data source. Generally reports are made from a query on data which has been saved for future use in a persistence data storage.[database/flat/xml file etc..]

Jasper Reporting is a template based architecture where the template file stores the report layout and data is later filled into this layout. The template file is a an xml file [.jrxml] and is holds the design of the report; data is provided to the complied layout [.jasper java object] and the jasper engine creates the report.

image sourced from :http://ireport.sourceforge.net/cap3.html

Software Requirements

For this Sample application i have used the following software’s.

  • JDK 1.6
  • iReport 3.0.0[or higher]
  • Netbeans IDE 6.5
  • Acrobat PDF reader
  • Jasper Report API jars

Setting up everything

Download and install JDK1.6,Netbeans IDE and iReport.

Start netbeans and create a Java Application project.

Jasper Report API Jars

Please download the following Jar files and import them in the library of the newly created java application project.


Note: All these jars will be present in the lib directory of the iReport.

Using a Database with JDBC connection

Data from a database can be converted to a report using jasper API in two ways.

  • Using iReport tool alone.
  • Using iReport for designing and using the design in a java program for report creation.

note: Assumption made is that the user already has an installed database.

Using a Database [JDBC connection] : Creating report suing iReport alone

Let’s create a report which reports employee information stored inside a table. The table has 3 fields and 3 rows of data stored.

Following are the steps needed to get the report generated from the iReport tool.

    1. Create a database connection so that iReport can us it to create the report
    • Open the Wizard [Data > Connections/DataSources]

    1. Create a template which reports 3 fields for report

Note: The Report has
    • A reportQuery is embedded into the report [Data > Report Query]

  • 3 defined Fields [EID,ENAME,ESALARY] this should be same as the column names [Or alias used in the ReportQuery]
  • 3 defined Fields [EID,ENAME,ESALARY] are placed in the detail band of the report[ EID is the selected TextField ]
  • All the other bands are made to nonexistent [Band height=0]
  • Finally Run the Report

Click the button marked in red circle

The output will look like[assuming that the preview selected is PDF]

Congrats you have your first Report from database!!.

email

«»

Comments

comments