Java Reporting - Part 2
Architecture:
As shown in the above figure Jasper architecture is based on defined XML files which by convention have an extension of jrxml this file contains the report layout template. A lot of third-party design tools were produced to generate your jrxml file in a smooth way (like iReport or JasperAssistant)
Design file is supposed to be filled by report's result which is fetched from database, XML files, Java collection, Comma-separated values or Models. Jasper can communicate with those data-sources and more, it can merge any number of data-sources together and manipulates the results of any combinations. This communication goes through JDBC, JNDI, XQuery, EJBQL, Hibernate or existing Oracle PL/SQL. You also can define your own data-source class and pass it to jasper engine directly.
After defining your report design layout in jrxml format and determining your data source(s) jasper engine does the rest of work. It compiles your design file and fills it with results fetched from data-source and generates your report to the chosen exporting format (PDF, Excel, HTML, XML, RTF, TXT …, etc.)
Report Definition file structure (jrxml):
Jasper design file –jrxml- contains the following elements:
- <jasperReport>: the root element.
- <title>: its contents are printed only once at the beginning of the report
- <pageHeader> - its contents are printed at the beginning of every page in the report.
- <detail> - contains the body of the report, repeated by n number of results
- <pageFooter> - its contents are printed at the bottom of every page in the report.
- <band> - defines a report section, all of the above elements contain a band element as its only child element.
Only the root
Environment:
To set up working environment we need to download JasperReport jar file from the following URL: http://sourceforge.net/project/showfiles.php?group_id=36382&package_id=28579
And add the following jars to your project classpath:
- jasperreports-2.0.4.jar
- commons-digester-1.7.jar
- commons-collections-2.1.jar (commons-collections.jar)
- commons-logging-1.0.2.jar
- commons-beanutils.jar
- iText-2.0.7.jar (used infor PDF exporting)
Sample application:
At this section we'll introduce a sample application that generates PDF, HTML and Excel files contain the results of our report which is built over Oracle database contains the following table:
ITEM ITEM_ID ---- NUMBER(5) --- NOT NULL |
Result: Report should retrieve the items with amount less than or equal 100 item.
We're going to divide the work into two steps:
1-Generate the report design (jrxml file).
2-Implement application that assigns data source, compiles jrxml file and exports result in the chosen format.
1- Designing report
First we create new text file and rename it to sample_report.jrxml, this file should contain the following XML tags.
<!DOCTYPE jasperReport PUBLIC "//JasperReports//DTD Report Design//EN" "http://jasperreports.sourceforge.net/dtds/jasperreport.dtd"> <jasperReport name="sample_report" > <queryString> <![CDATA[select item_name,item_amount from item where item_amount <=100]]> </queryString> <field name="ITEM_NAME" class="java.lang.String"/> <field name="ITEM_AMOUNT" class="java.math.BigDecimal"/> <columnHeader> <band height="28" isSplitAllowed="true"> <staticText> <reportElement x="40" y="11" width="193" height="15" key="staticText-1"/> <text> <![CDATA[Item Name]]> </text> </staticText> <staticText> <reportElement x="330" y="11" width="193" height="15" key="staticText-2"/> <text> <![CDATA[Item Amount]]> </text> </staticText> </band> </columnHeader> <detail> <band height="27" isSplitAllowed="true"> <textField> <reportElement x="47" y="6" width="173" height="18" key="textField"/> <textFieldExpression class="java.lang.String"> <![CDATA[$F{ITEM_NAME}]]> </textFieldExpression> </textField> <textField > <reportElement x="330" y="6" width="100" height="18" key="textField"/> <textFieldExpression class="java.math.BigDecimal"> <![CDATA[$F{ITEM_AMOUNT}]]> </textFieldExpression> </textField> </band> </detail> </jasperReport> |
The above XML file consists of the following main sections that defining report behavior and layout:
- <queryString>: contains the SQL statement which retrieves the report result.
- <field name>: defines the resulted fields from the query, and give them name to reuse them into the report body [they are case-sensitive].
- <staticText>: contains the header titles "Item Name" in <![CDATA[Item Name]]> tag format.
- <textFieldExpression>: defines the appearance of result field.
- $F{ITEM_NAME}: is a variable contains the value of Query result predefined field in the tag <field name>.
Once we finished the report design file, save it in C:\ directory.
2- Implementing report service:
- Create a new java project.
- Import the jars listed in environment section to your project libraries.
- Create new class and import the following packages
import java.sql.Connection; import java.util.HashMap; import net.sf.jasperreports.engine.JRException; |
- Define the data source, in my case it's an oracle connection and established by JDBC as following:
public static Connection establishConnection() |
Finally, the core code for compiling, filling and exporting the results in the following sequence:
- Define jasper objects that will hold report template, compiled files, and result files.
/* JasperReport is the object |
- Create a connection to my data-source; initialize the report parameter into empty HashMap then compile our jrxml file into JasperReport object and finally fill the JasperPrint object by data from data-source connection.
// connection is the data source we used to fetch the data from // jrxml compiling process // filling report with data from data source jasperPrint = JasperFillManager.fillReport(jasperReport,jasperParameter, connection); |
- Last segment of code is responsible for exporting the result files into different formats
// exporting process // 2- export to HTML // 3- export to Excel sheet |
Congratulation, you've just managed to generate your first jasper report in 3 different file formats at C:\\ directory (shown in the image below):
- sample_report.html
- sample_report.pdf
- sample_report.xls
Here we reach the end of today's article, next article we will cover the following points:
1- Using design tool (iReport) to generate robust and smooth jrxml file.
2- Create run-time search criteria and passing parameters to report.
2 comments:
Wonderful blog! I found it while surfing around on Yahoo News. Do you have any tips on how to get listed in Yahoo News? I've been trying for a while but I never seem to get there! Many thanks
Hi friends, how is all, and what you wish for to say concerning this article, in my view its really amazing in support of me.
Post a Comment