Jasper Reports Data Sources - JasperReports

What are the Data Sources in Jasper Reports?

The structured data containers of the Jasper Reports are known s Data sources. The data sources facilitate the data to the JasperReports engine in the process of generating reports. The data is obtained from different sources like databases, XML files, arrays and collection of objects. As discussed in the previous chapter of Filling Reports the fillReportXXX () method expects to receive a data source of the report, which has to fill, in the form of net.sf.jasperreports.engine.JRDataSource object or a java.sql.Connection (when the report data is found in a relational database).

The JRDataSource interface has only two methods, which should be implemented −

  • public boolean next() throws JRException -At the report filling time, this method is called on the data source object by the reporting engine when iterating through the data.
  • public Object getFieldValue(JRField jrField) throws JRException -This method provides the value for each report field in the current data source record.

The data is retrieved from the data source by using the report field. Several default implementations of the JRDataSource interface are available, depending on which, the records of the data source are acquired.

What are the different Datasource Implementation classes in JasperReports?

The datasources and the implementation classes are provided in the table as follows:

Datasource
Implementation Class
JDBC
net.sf.jasperreports.engine.JRResultSetDataSource
JavaBean
net.sf.jasperreports.engine.data.JRBeanCollectionDataSource, net.sf.jasperreports.engine.data.JRBeanArrayDataSource
Map-based
net.sf.jasperreports.engine.data.JRMapArrayDataSource, net.sf.jasperreports.engine.data.JRMapCollectionDataSource
TableModel
net.sf.jasperreports.engine.data.JRTableModelDataSource
XML
net.sf.jasperreports.engine.data.JRXmlDataSource
CSV
net.sf.jasperreports.engine.data.JRCsvDataSource
XLS
net.sf.jasperreports.engine.data.JRXlsDataSource
Empty
net.sf.jasperreports.engine.JREmptyDataSource

JDBC Data Sources

Class JRResultSetDataSource craps a java.sql.ResultSet object. This is the most commonly used data source implementations when report data are extracted from a relational database. If a java.sql.Connection is passed to the engine instead, it executes first the related query and stores the returned java.sql.ResultSet object in a JRResultSetDataSource instance.

JavaBean Data Sources

Classes JRBeanArrayDataSource and JRBeanCollectionDataSourcerepresent implementations that can wrap arrays and collections of JavaBean objects. Each object inside the array or the collection will be seen as one record in this type of data source. The mapping between a particular JavaBean property and the corresponding report field is made by naming conventions. The name of the report field must be the same as the name of the JavaBean property as specified by the JavaBeans specifications.

In all the examples JRBeanCollectionDataSource is used.

Map-based Data Sources

The implementation classes JRMapArrayDataSource and JRMapCollectionDataSource are useful if the parent application already stores the reporting data available in-memory as java.util.Map objects. Each Map object in the wrapped array or collection is considered a virtual record in the data source, and the value of each report field is extracted from the map using the report field named as the key.

TableModel Data Sources

In many client-side applications, data is displayed in tabular format. A common requirement in many applications is to allow the user to print this tabular format as a report. Implementation class JRTableModelDataSourcemakes the task of generating reports from tabular format trivial for Swing applications. This class wraps a javax.swing.table.TableModel object. Columns in the wrapped TableModel object can be accessed either by their names or by their 0-based indexes.

XML Data Sources

Class JRXmlDataSource is a data source implementation based on DOM, which uses XPath expressions to select data from the XML document. Records in the XML data source are represented by node elements selected through the XPath expression. Field values are retrieved from each record using the XPath expression provided by the field description (<fieldDescription> element in JRXML).

XPath is a language used to navigate through an XML document's attributes and elements. More information about XPath can be found at the link - http://www.w3.org/TR/xpath.

CSV Data Sources

JRCsvDataSource represents an implementation for data sources, which retrieve their data from structured text files; usually CSVs. Field values are retrieved using their column index.

XLS Data Sources

JRXlsDataSource represents an implementation for data sources, which retrieve their data from Excel documents. Report-field mapping for this data source implementation is also based on the field column index.

Empty Data Sources

The class JREmptyDataSource, simulates a data source with a given number of virtual empty records inside. It is used by the UI tools to offer basic report preview functionality, or in special report templates, or for testing and debugging purposes.

Rewindable Data Sources

The net.sf.jasperreports.engine.JRRewindableDataSource extends the basic JRDataSource interface. It adds only one method, called moveFirst (), to the interface. This method is intended to move the cursor to the first element in the datasource.

Rewindable data sources are useful when working with sub-reports placed inside a band that is not allowed to split due to the isSplitAllowed="false" setting and there is not enough space on the current page for the sub report to be rendered.

The above discussed implementations are all rewindable except for the JRResultSetDataSource, as it will not support moving of the record pointer to back. This may lead to problems like if the data source is used manually to wrap a java.sql.ResultSet prior to the passing of it to the sub-report.

What are Data Source Providers in JasperReports?

An interface net.sf.jasperreports.engine.JRDataSourceProvider, is provided by the JasperReports, which helps in creating and disposing the data source objects. The report template is created by using the GUI tools which are used for customizing the report’s data source. JRDataSourceProvider is the standard way to plug custom data sources into a design tool. A custom implementation of this interface should implement the following methods that allow creating and disposing of data source objects and also methods for listing the available report fields inside the data source if possible −

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

JasperReports Topics