3.2. Data Sources

The Data Source term is defined as a source of data used by the reporting engine to execute a report and obtain the final document. In JasperAssistant, data sources are used to preview reports that are designed using the report editor. The following data source types are supported:

Figure 3.1. Data source configuration page

Data source configuration page

3.2.1. Empty Data Source

Empty Data Source is a general purpose data source that can be used to perform a quick test of a report. This data source can be used instead of data sources types that are not available in JasperAssistant. Empty Data Source allows the report to be filled with a set of empty rows, with all fields having null values.

Figure 3.2. Empty data source configuration dialog

Empty data source configuration dialog

The following information must be provided for an Empty Data Source:

  • Name

    Name of the data source. Required for identification purposes.

  • Size

    Number of empty rows supplied by the data source. Must be 0 or greater.

Tip

When using JasperReports API directly, the same data source can be created by using the net.sf.jasperreports.engine.JREmptyDataSource class.

3.2.2. Database Data Source

Database Data Source is the data source that extracts necessary data from a database by using an SQL query or a call to a stored procedure. This data source requires a database specific JDBC driver to connect to the database. Please refer to the documentation of the JDBC driver to find out the exact values and format of parameters required by the data source configuration.

Figure 3.3. Database data source configuration dialog

Database data source configuration dialog

The following information must be provided for a Database Data Source:

  • Name

    Name of the data source. Required for identification purposes.

  • Driver

    Full name of the Java class that implements the JDBC driver interface. This is specific for each JDBC driver.

    Example 3.1. Driver for Hypersonic Database

    org.hsqldb.jdbcDriver

  • URL

    Database connection URL. This is specific for each JDBC driver.

    Example 3.2. URL for Hypersonic Database

    jdbc:hsqldb:hsql://localhost

  • Username

    Username that is used to connect to the database.

  • Password

    Optional password that is used together with supplied user name to connect to the database.

  • Verify password

    Must contain the same value as the Password property. This ensures that the password is entered correctly.

  • JAR file 1

    Location of the JAR archive containing the JDBC driver. This file is required for drivers that are not available at runtime. For example this is not required for jdbc:odbc bridge driver that is used to connect to ODBC data sources (Access files, etc).

  • JAR file 2

    Location of an additional JAR archive. It is required for JDBC drivers that are distributed in several JAR files. An example of such a driver is Microsoft JDBC Driver for SQL Server.

  • JAR file 3

    Location of an additional JAR archive. It is required for JDBC drivers that are distributed in several JAR files. An example of such a driver is Microsoft JDBC Driver for SQL Server.

Use Test... button to test that the database connection settings are entered correctly. The test is performed by attempting to open a database connection.

Tip

When using JasperReports API directly, the same data source can be created by using the net.sf.jasperreports.engine.JRResultSetDataSource class.

3.2.3. XML Data Source

XML Data Source is the data source that extracts necessary data from an XML document. XML Data Source uses XPath select expressions to extract data from the document.

Figure 3.4. XML data source configuration dialog

XML data source configuration dialog

The following information must be provided for an XML Data Source:

  • Name

    Name of the data source. Required for identification purposes.

  • XML file

    Location of the file containing the XML document.

  • XPath select

    XPath selection expression that supplies the node-set (rows) for the data source.

XML Data Source requires additional information to be supplied in report fields. Report fields must have the Description property set to an XPath select expression. These expressions are used to extract individual field values from the current node of the document. Refer to the "xmldatasource" sample coming with JasperReports distribution for a concrete example.

Tip

Short XPath tutorial with examples:

http://www.zvon.org/xxl/XPathTutorial/General/examples.html

Tip

When using JasperReports API directly, the same data source can be created by using the net.sf.jasperreports.engine.data.JRXmlDataSource class.

3.2.4. Custom Data Source

Custom Data Source is a mechanism that allows the user to plug in custom data sources that are not directly supported by JasperAssistant. Generally, such data sources require a specific Java initialization code.

Figure 3.5. Custom data source configuration dialog

Custom data source configuration dialog

The following information must be provided for a Custom Data Source:

  • Name

    Name of the data source. Required for identification purposes.

  • Java project

    Java project that contains the data source provider class.

  • Provider class

    Full name of the Java class that implements the net.sf.jasperreports.JRDataSourceProvider interface. For more details on implementation see the Java Doc for this interface.

Tip

JasperReports provides a base class for provider implementations that supply Java bean collection or array data sources. This class is:

net.sf.jasperreports.data.JRAbstractBeanDataSourceProvider

This base class implements a mechanism that allows the bean properties to be auto-discovered by JasperAssistant when using the Field Wizard.

Example 3.3. An implementation of data source provider interface

import java.util.*;
import net.sf.jasperreports.engine.*;
import net.sf.jasperreports.engine.data.*;

/**
 * Data source provider implementation that
 * provides a bean collection data source
 * containing instances of Person class.
 */
public class ExampleDataSourceProvider extends 
        JRAbstractBeanDataSourceProvider {

public ExampleDataSourceProvider() {
    super(Person.class);
}

public JRDataSource create(JasperReport report)
        throws JRException {
        
    ArrayList collection = new ArrayList();
    collection.add(new Person("Teodor", "Danciu"));
    collection.add(new Person("Peter", "Severin"));
    return new JRBeanCollectionDataSource(collection);
}

public void dispose(JRDataSource dataSource) 
        throws JRException {
    // nothing to dispose
}

}