Java Reference
In-Depth Information
The following table illustrates the parameters used in these methods:
Parameter
Description
This is used as the report template. Instances of
net.
sf.jasperreports.engine.JasperReport
are in-
memory representations of compiled report templates.
JasperReport jasper-
Report
This is used to define the name of the destination file to
which to save the report.
String destFileName
This is an instance of a class implementing the
java.
util.Map
interface. It is used to initialize all report
parameters defined in the report template.
Map parameters
Connection connection
This is used to connect to a database, in order to execute an
SQL query defined in the report template.
This is an instance of a class implementing the
net.
sf.jasperreports.engine.JRDataSource
interface.
JasperReports provides several implementations of the
JRDataSource
interface. Moreover, we are also free to
create our own custom implementations.
JRDataSource
dataSource
This is used to indicate the file system location of the
compiled report template. The generated report is saved
using the same name and location as the given compiled
report, substituting the
.jasper
extension with .
jrprint
.
String sourceFileName
As can be seen above, in most cases, we pass data for filling reports via an instance of
a class implementing the
net.sf.jasperreports.engine.JRDataSource
interface.
Report templates can have embedded SQL queries. These SQL queries are defined
inside the
<queryString>
element in the JRXML file. For reports that contain an
SQL query, instead of passing a
JRDataSource
, we pass an instance of a class
implementing the
java.sql.Connection
interface. JasperReports then uses this
Connection
object to execute the query and obtain the report data from the database.
Our report contains only static text; there is no dynamic data displayed in the
report. There is no way to fill a report without passing either a
JRDataSource
or
a
Connection
. JasperReports provides an implementation of the
JRDataSource
containing no data. The class is named, appropriately enough,
JREmptyDataSource
.
Since our report takes no parameters, passing an empty instance of
java.util.
HashMap
will be enough for our purposes. We will follow the recommended
approach of naming our report using the same name as the one used for the report
template (except for the extension). Given all of these facts, the most appropriate
version of
fillReportToFile()
for our report is the fourth version. Here is its
signature again: