English 中文(简体)
JasperReports - Data Sources
  • 时间:2024-11-03

Report Data Sources


Previous Page Next Page  

Datasources are structured data container. While generating the report, JasperReports engine obtains data from the datasources. Data can be obtained from the databases, XML files, arrays of objects, and collection of objects. We saw in the chapter Filpng 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 −

    pubpc boolean next() throws JRException;

      At the report filpng time, this method is called on the data source object by the reporting engine when iterating through the data.

    pubpc Object getFieldValue(JRField jrField) throws JRException;

      This method provides the value for each report field in the current data source record.

The only way to retrieve data from the data source is by using the report fields. There are several default implementations of the JRDataSource interface, depending on the way, the records in the data source are acquired.

Datasource Implementations

The table given below summarizes the datasources and their implementation classes −

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 JRBeanCollectionDataSource represent 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 of this tutorial, we have used JRBeanCollectionDataSource.

Map-based Data Sources

The implementation classes JRMapArrayDataSource and JRMapCollectionDataSource are useful if the parent apppcation 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 cpent-side apppcations, data is displayed in tabular format. A common requirement in many apppcations is to allow the user to print this tabular format as a report. Implementation class JRTableModelDataSource makes the task of generating reports from tabular format trivial for Swing apppcations. 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 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 functionapty, 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 sppt due to the isSpptAllowed="false" setting and there is not enough space on the current page for the sub report to be rendered.

All the above data source implementations are rewindable except for the JRResultSetDataSource, as it does not support moving the record pointer back. This poses a problem only if this data source is used manually to wrap a java.sql.ResultSet before passing it to the sub-report. There is no problem, if the SQL query resides in the sub-report template, as the engine will execute it again when restarting the sub-report on the next page.

Data Source Providers

The JasperReports pbrary has an interface net.sf.jasperreports.engine.JRDataSourceProvider. This helps in creating and disposing of data source objects. When creating a report template using GUI tools, a special tool for customizing the report s data source is needed. 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 psting the available report fields inside the data source if possible −

pubpc boolean supportsGetFieldsOperation();

pubpc JRField[] getFields(JasperReport report)
   throws JRException, UnsupportedOperationException;

pubpc JRDataSource create(JasperReport report) throws JRException;

pubpc void dispose(JRDataSource dataSource) throws JRException;
Advertisements