Java Reference
In-Depth Information
Dynamically modifying report queries is only one of the many possible uses of report
parameters. Report parameters are discussed in more detail in the next chapter.
Database Reporting via a Datasource
Another way to generate reports based on database data is by using a datasource.
In JasperReports terminology, a datasource is a class implementing the
net.
sf.jasperreports.engine.JRDataSource
interface.
To use a database as a datasource, the JasperReports API provides the
net.
sf.jasperreports.engine.JRResultSetDataSource
class. This class implements
JRDataSource
and has a single public constructor that takes
java.sql.ResultSet
as its only parameter.
JRResultSetDataSource
provides no public methods or
variables. To use it, all we need to do is provide a result set to its constructor and
pass it to the report via the
JasperFillManager
class.
Let us modify the above JRXML template so that it uses a
JRResultSetDataSource
to obtain database data.
The only change we need to make on the JRXML template is to remove the
<queryString>
element.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE jasperReport PUBLIC "//JasperReports//DTD Report Design//EN"
"http://jasperreports.sourceforge.net/dtds/jasperreport.dtd">
<jasperReport name="DbReportDS">
<field name="tail_num" class="java.lang.String"/>
<field name="aircraft_serial" class="java.lang.String"/>
<field name="aircraft_model" class="java.lang.String"/>
<field name="engine_model" class="java.lang.String"/>
<pageHeader>
<band height="30">
<staticText>
<reportElement x="0" y="0" width="69" height="24"/>
<textElement verticalAlignment="Bottom"/>
<text>
<![CDATA[Tail Number: ]]>
</text>
</staticText>
<staticText>
<reportElement x="140" y="0" width="69" height="24"/>
<text>