http://www.javaworld.com/javaworld/jw-09-2002/jw-0920-opensourceprofile.html
Generating reports is a common, if not always glamorous, task for programmers. In the past, report generation has largely been the domain of large commercial products such as Crystal Reports. Today, the open source JasperReports report generating library gives Java developers a viable alternative to commercial software.
JasperReports provides the necessary features to generate dynamic reports, including data retrieval using JDBC (Java Database Connectivity), as well as support for parameters, expressions, variables, and groups. JasperReports also includes advanced features, such as custom data sources, scriptlets, and subreports. All in all, JasperReports combines good features, maturity, community participation, and, best of all, it's free.
This article kicks off JavaWorld's new Open Source Profile column dedicated to Java-based open source tools and components. Look for upcoming articles spotlighting the Echo Web application framework and ObJectRelationalBridge, an object/relational mapping tool. Feel free to send me your suggestions for future articles.
Note: The documentation and code featured in this article are based on JasperReports version 0.3.3.
Report design
In JasperReports, you design reports using XML report templates. For example, the following XML file is a template for a report with a title, two columns of data, and page numbers:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE jasperReport PUBLIC "-//JasperReports//DTD Report Design//EN"
"http://jasperreports.sourceforge.net/dtds/jasperreport.dtd">
<jasperReport name="BasicReport" >
<parameter name="Title" class="java.lang.String"/>
<queryString><![CDATA[select name, cost from product]]></queryString>
<field name="NAME" class="java.lang.String"/>
<field name="COST" class="java.lang.Double"/>
<title>
<band height="50">
<textField>
<reportElement x="0" y="0" width="200" height="50" />
<textFieldExpression class="java.lang.String">$P{Title}</textFieldExpression>
</textField>
</band>
</title>
<pageHeader>
<band>
</band>
</pageHeader>
<columnHeader>
<band height="20">
<staticText>
<reportElement x="180" y="0" width="180" height="20"/>
<textElement>
<font isUnderline="true"/>
</textElement>
<text><![CDATA[NAME]]></text>
</staticText>
<staticText>
<reportElement x="360" y="0" width="180" height="20"/>
<textElement>
<font isUnderline="true"/>
</textElement>
<text><![CDATA[COST]]></text>
</staticText>
</band>
</columnHeader>
<detail>
<band height="20">
<textField>
<reportElement x="180" y="0" width="180" height="20"/>
<textFieldExpression class="java.lang.String"><![CDATA[$F{NAME}]]></textFieldExpression>
</textField>
<textField pattern="0.00">
<reportElement x="360" y="0" width="180" height="20"/>
<textFieldExpression class="java.lang.Double"><![CDATA[$F{COST}]]></textFieldExpression>
</textField>
</band>
</detail>
<columnFooter>
<band>
</band>
</columnFooter>
<pageFooter>
<band height="15">
<staticText>
<reportElement x="0" y="0" width="40" height="15"/>
<textElement/>
<text><![CDATA[Page:]]></text>
</staticText>
<textField>
<reportElement x="40" y="0" width="100" height="15"/>
<textElement/>
<textFieldExpression class="java.lang.Integer"><![CDATA[$V{PAGE_NUMBER}]]></textFieldExpression>
</textField>
</band>
</pageFooter>
<summary>
<band>
</band>
</summary>
</jasperReport>
The template's beginning includes any parameters passed into the report, the query that retrieves the data for the report, and the fields displayed in the report. The template's remainder divides into six report sections: