Data source

The important part of reporting is the data source which fills the report design with given data. Data can be retrieved from various sources such as a database, a xml file or using an interface JRDataSource. JasperReports itself has a lot of implementations of JRDataSource interface and basically implementation of this interface by you is quite often unneeded.
Data in reports can be additionally sorted or filtered and some components such as chart, subreport, and crosstab can have their own data source if it's needed. Please refer to the link below for examples.
Configuration options

methoddescription
setFilterExpression(DRIExpression<Boolean> filterExpression) Sets a dataset filter expression. The expression must be a type of Boolean
sortBy(TextColumnBuilder<?> ...sortColumns),
sortBy(SortBuilder ...sorts),
addSort(SortBuilder ...sorts)
Adds a sort field to the dataset

Examples
DataSource examples

Database

In this type of data source, data are retrieved from a database. You only need a properly configured database connector and a SQL query.
Configuration options

methoddescription
setDataSource(String sql, Connection connection),
setDataSource(QueryBuilder query, Connection connection),
setDataSource(ResultSet resultSet)
Sets a database data source

Examples
Quick usage:

java.sql.Connection connection = ...;
String query = "SELECT * FROM ...";
report()
  .setDataSource(query, connection) 
 
Another example: DatabaseDatasourceReport

Data source object

In this type, a JRDataSource object is expected as a data source. A good explanation of various implementations of data sources, as well as a few examples can be found using the following link: JasperReports data sources
Configuration options

methoddescription
setDataSource(JRDataSource dataSource),
setDataSource(Collection<?> collection)
Sets a data source object

Examples
Quick usage:

List<JavaBean> data = new ArrayList<JavaBean>();
report()
  .setDataSource(new JRBeanCollectionDataSource(data)) 
 
Another example: CollectionDatasourceReport

Tags: datasource

Latest News

  • Thu
    Jan
    25
    Changes in version 5.1.0 upgrade to JasperReports 6.5.1 removed xhtml and excelapi exporter removed thread safety...
  • Fri
    Jan
    27
    Changes in version 5.0.0 upgrade to JasperReports 6.4.0 upgrade to Java 1.7, DynamicReports is no longer compatibl...
  • Thu
    Jul
    28
    Changes in version 4.1.1 upgrade to JasperReports 6.2.2 added new types of component stretch type minor bug fixes...