Java Reporting With Jasper Reports - Part 2
Join the DZone community and get the full member experience.
Join For FreeWelcome back to Java Reporting series. For those who didn't read the introductory article; have a look before we get started. Today we're going to have a quick tour in JasperReport architecture, development lifecycle, report definition files, and finally we're going to set up our environment and start work in a sample application.
Architecture
As shown in the above figure JasperReports architecture is based on declarative XML fileswhich by convention have an extension of jrxml that contains the report layout. 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 element is mandatory, the rest of elements are optional.
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:
-
Generate the report design (jrxml file).
-
Implement application that assigns data source, compiles jrxml file and exports result in the chosen format.
Designing The 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.
Implementing The 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.sql.DriverManager; import java.sql.SQLException; import java.util.HashMap; import net.sf.jasperreports.engine.JRException; import net.sf.jasperreports.engine.JRExporterParameter; import net.sf.jasperreports.engine.JasperCompileManager; import net.sf.jasperreports.engine.JasperExportManager; import net.sf.jasperreports.engine.JasperFillManager; import net.sf.jasperreports.engine.JasperPrint; import net.sf.jasperreports.engine.JasperReport; import net.sf.jasperreports.engine.export.JRXlsExporter;
- Define the data source, in my case it's an oracle connection and established by JDBC as following:
public static Connection establishConnection() { Connection connection = null; try { Class.forName("oracle.jdbc.driver.OracleDriver"); String oracleURL = "jdbc:oracle:thin:@localhost:1521:mySID"; connection = DriverManager.getConnection(oracleURL,"username","password"); connection.setAutoCommit(false); } catch(SQLException exception) { exception.printStackTrace(); } return connection; }
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 that holds our compiled jrxml file */ JasperReport jasperReport; /* JasperPrint is the object contains report after result filling process */ JasperPrint jasperPrint;
- 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 Connection connection = establishConnection(); // jasperParameter is a Hashmap contains the parameters // passed from application to the jrxml layout HashMap jasperParameter = new HashMap(); // jrxml compiling process jasperReport = JasperCompileManager.compileReport ("C://sample_report.jrxml"); // 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 // 1- export to PDF JasperExportManager.exportReportToPdfFile(jasperPrint, "C://sample_report.pdf"); // 2- export to HTML JasperExportManager.exportReportToHtmlFile(jasperPrint, "C://sample_report.html" ); // 3- export to Excel sheet JRXlsExporter exporter = new JRXlsExporter(); exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, "C://simple_report.xls" ); exporter.exportReport();
You have 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 pass them to report.
Opinions expressed by DZone contributors are their own.
Comments