Creating the JasperReport design file
The JasperReport design file specifies the layout and appearance of the report. Unless you import a working design file (with the extension .jasper), you will need to create or modify one.
You can use a file with the .xml extension for your source, although this can slow your compilation. JasperReports works best with the .jrxml extension.
- Create a design document in one of the following ways:
- Use a third-party JasperReports design tool (like JasperAssistant or iReport). Make sure the file you create has a .jrxml extension.
- Use a text editor to write JasperReports XML source information into a new text file and save the file as a .jrxml file.
- Place the XML design document in the same EGL package as your report driver file and optional EGL report handler.
- Customize the XML source file to use one of the following data
sources:
- When you have a fairly simple, straightforward database query, create a report of the type DataSource.databaseConnection. Include your SQL query in the XML design file source. The EGL report driver passes your connection information to JasperReports.
- When you need to perform complex database operations, or need to build your SQL statement dynamically, create a report of the type DataSource.sqlStatement. The EGL report driver includes your SQL query and passes the result set to JasperReports.
- When your data comes from somewhere other than a database, create a report of the type DataSource.reportData. The EGL report driver passes complete report data to JasperReports; no connection information is necessary.
- Compile the source file.
An example of a report design file follows:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE jasperReport PUBLIC
"//JasperReports//DTD Report Design//EN"
"http://jasperreports.sourceforge.net/dtds/jasperreport.dtd">
<jasperReport name="simpleReport">
<field name="CUSTOMER_ID" class="java.lang.String" />
<field name="FIRST_NAME" class="java.lang.String" />
<field name="LAST_NAME" class="java.lang.String" />
<field name="PHONE" class="java.lang.String" />
<pageHeader>
<band height="30">
<staticText>
<reportElement x="0" y="0" width="70" height="24" />
<text>
<![CDATA[Customer ID: ]]>
</text>
</staticText>
<staticText>
<reportElement x="140" y="0" width="70" height="24" />
<text>
<![CDATA[First name: ]]>
</text>
</staticText>
<staticText>
<reportElement x="280" y="0" width="70" height="24" />
<text>
<![CDATA[Last name: ]]>
</text>
</staticText>
<staticText>
<reportElement x="420" y="0" width="70" height="24" />
<text>
<![CDATA[Phone: ]]>
</text>
</staticText>
</band>
</pageHeader>
<detail>
<band height="30">
<textField>
<reportElement x="0" y="0" width="70" height="24" />
<textFieldExpression>
<![CDATA[$F{CUSTOMER_ID}]]>
</textFieldExpression>
</textField>
<textField>
<reportElement x="140" y="0" width="70" height="24" />
<textFieldExpression>
<![CDATA[$F{FIRST_NAME}]]>
</textFieldExpression>
</textField>
<textField>
<reportElement x="280" y="0" width="70" height="24" />
<textFieldExpression>
<![CDATA[$F{LAST_NAME}]]>
</textFieldExpression>
</textField>
<textField>
<reportElement x="420" y="0" width="70" height="24" />
<textFieldExpression>
<![CDATA[$F{PHONE}]]>
</textFieldExpression>
</textField>
</band>
</detail>
</jasperReport>
This example report design file prints four columns of information:
an ID number, a first name, a last name, and a phone number. The <pageHeader> section
prints column headers, and the <detail> section
prints rows of data based on the data provided by the report driver
program.
The following sections offer specifics for the different types of XML source files. This information covers very simple cases; for more complex examples see either the JasperReports website mentioned earlier or the documentation for your design tool (if you decide to use one).
EGL source files of the type DataSource.databaseConnection
<queryString><![CDATA[SELECT * FROM Table_Name]]></queryString> - Table_Name
- Name of a table from your database
<field name="Field_Name" class="java.lang.class_type"></field>- Field_Name
- A column name in the result set from the query in your design file. The field names must conform to Java™ variable name conventions. You can use aliases for column names within your SQL statement to handle duplicate names, illegal characters (such as "."), or other conflicts.
- Class_Type
- A java.lang class, such as Integer or String, that identifies the type of data to which Field_Name refers
<textFieldExpression class="java.lang.class_type">
<![CDATA[$F{Field_Name}]]>
</textFieldExpression>EGL source files of the type DataSource.sqlStatement
<field name="Field_Name" class="java.lang.class_type"></field>- Field_Name
- A column name in the result set that was created by the query in your EGL report driver. The field names must conform to Java™ variable name conventions. You can alias the column names within your SQL statement if necessary.
- Class_Type
- A java.lang class such as Integer or String, identifying the type of data to which Field_Name refers
<textFieldExpression class="java.lang.class_type">
<![CDATA[$F{Field_Name}]]>
</textFieldExpression>EGL source files of the type DataSource.reportData
<field name="Field_Name" class="java.lang.class_type"></field>- Field_Name
- The name of a field exactly as you specify it in your EGL source file
- Class_Type
- A java.lang class such as Integer or String, identifying the type of data to which Field_Name refers
<textFieldExpression class="java.lang.class_type">
<![CDATA[$F{Field_Name}]]>
</textFieldExpression>Compiling the XML design file source
- The .jrxml file has changed
- The .jrxml file is free of errors
- The javac compiler is in your execution path
EGL places the compiled .jasper file in the Java™ Resources\package_name directory that is parallel to EGLSource\package_name. When you successfully generate your EGL report driver, the product places a linked copy of the .jasper file in the parallel bin\package_name directory. You can manually create and copy the .jasper file by selecting or .
For guidelines on creating an XML design document and a report handler simultaneously, see Creating reports with JasperReports. For an example that shows how an XML design document gets a report data record from the report handler, see Writing code to drive a report of type JasperReport.