A guide to Java, Linux, and other Technology topics

Creating Database Reports With JasperReports

Creating The JRXML File

As explained in our previous article, the XML definition file for a JasperReports report is called a JRXML file. The JRXML file for our database report is defined as follows:

<?xml version="1.0"?>
<!DOCTYPE jasperReport
  PUBLIC "-//JasperReports//DTD Report Design//EN"
  "https://jasperreports.sourceforge.net/dtds/jasperreport.dtd">

<jasperReport name="Database_Report">
   <field name="course_cd" class="java.lang.String" />
   <field name="course_nm" class="java.lang.String" />
   <field name="first_nm" class="java.lang.String" />
   <field name="last_nm" class="java.lang.String" />
   <detail>
      <band height="20">
         <textField>
            <reportElement x="10" y="0" width="600" height="20" />
            <textFieldExpression class="java.lang.String">
               <![CDATA[$F{course_cd}]]>
            </textFieldExpression>

         </textField>
         <textField>
            <reportElement x="80" y="0" width="200" height="20" />
            <textFieldExpression class="java.lang.String">
               <![CDATA[$F{course_nm}]]>
            </textFieldExpression>

         </textField>
         <textField>
            <reportElement x="280" y="0" width="200" height="20" />
            <textFieldExpression class="java.lang.String">
               <![CDATA[$F{first_nm}]]> + " " + <![CDATA[$F{last_nm}]]>
            </textFieldExpression>

         </textField>
      </band>
   </detail>
</jasperReport>

Relevant sections of the JRXML file for database reporting are shown in bold. The name of the <field> elements must map database columns in the query used to gather data for the report. The class attribute of the <field> elements must correspond to the appropriate class of the data in the result set for the query.

These fields are then accessed using the $F{fieldName} syntax inside the <textFieldExpression> elements, as shown in the example above. The generated report will have a line for each row returned in the query.