Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Reports made easy with JasperReports

The open source JasperReports uses XML templates for your reporting needs

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone

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:

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone
Comments (19)
Login
Forgot your account info?

everyone that has used dot(...By Anonymous on December 30, 2009, 5:54 ameveryone that has used dot(.) in a field name has died of ococodiasis. I wouldn't do that if I was you ... :-)

Reply | Read entire comment

Java programmerBy Anonymous on December 29, 2009, 5:14 amImprove on java programming

Reply | Read entire comment

1malaysiaBy Anonymous on December 24, 2009, 3:39 am1malaysia!

Reply | Read entire comment

xxxBy Anonymous on September 27, 2009, 6:11 amxxx

Reply | Read entire comment

EclipseBy Anonymous on September 24, 2009, 10:43 amin eclipse download the jasperAssistant plugin

Reply | Read entire comment

View all comments

Add comment
Anonymous comments subject to approval. Register here for member benefits.
Have a JavaWorld account? Log in here. Register now for a free account.
Resources
  • Open source alternatives to JasperReports include: