Home >Documents >Chapter6 Part2 - Reporting

Chapter6 Part2 - Reporting

Date post:26-Oct-2014
View:71 times
Download:5 times
Share this document with a friend

JasperReportsJasperReports is an open-source Java class library designed to aid developers with the task of adding reporting capabilities to Java applications. It can be used to deliver dynamic content to the screen or printer. It can create reports in multiple formats, such as HTML file, PDF file, Excel file, CSV file , XML file, etc. It is capable of producing a report using data from a database, parameters, variables expressions and groups, etc. It also includes features, such as chart, crosstab, custom data sources, scriptlet, and subreport.


JasperReportsJasperReport is a framework or class library where developers have to create report template by writing all the XML codes (or more accurately jrxml) from scratch.


JasperReportsJasperReport uses XML file to define the report template. It is a bit difficult to define the report template by using purely XML code. Luckily we have iReport report designer, which provides us the ability to drag and drop to create report, no XML code writing needed. Note that iReport does not replace JasperReport, it is merely enhance the developer productivity in creating report template. We still require JasperReport framework or class library to provide us those API in order to compile and execute report.




Pre-requisiteTo use Jasper reporting, you need 1. Netbeans IDE 6.0 or newer


JasperReports-3.5.2 class libraryiReport 3.5.2 designer

JasperReports and iReport can be downloaded at http://sourceforge.net.

Note: Newer versions (3.5.3, 3.6.0, 3.6.1, 3.6.2) can be found at the Website but may not work. You may use them if they work on your system.Note: It is better to use the same version for both JasperReports and iReport. E.g., use JasperReport-3.6.1 with iReport 3.6.1.


Download JasperReportYou may browse the Web site to download jasperreports-3.5.2-project.zip or follows the direct link: http://sourceforge.net/projects/jasperreports/files/1.jasperreports/


Download iReportYou may browse the Web site to download iReport-nb-3.5.2-windows-installer.exe or follows the direct link: http://sourceforge.net/projects/ireport/files/ Download the standalone version.


iReport: Installation and SetupInstall iReport and run it. Click Step 1 to setup database connection.

Then, click Next.


iReport: Installation and SetupEnter your database name here. Select JDBC-ODBC (sun.jdbc.odbc. Enter your database name here. Click Test. Bridge JdbcOdbcDriver).

If connection test successful, click Save.


iReport: Creating ReportBefore proceed to Step 2, it is better to create a folder to keep all your reports. Create a folder (use Windows Explorer) called Reports (or other names) in your existing project folder. Now, click Step 2 in the iReports Welcome Window and enter your report name here. Browse for Reports folder in your project folder. Click Next.


iReport: Creating ReportSelect the database connection you have created in Step 1. Enter your SQL statement.

Click Next.


iReport: Creating ReportFields may be added one by one or all together.


iReport: Creating ReportGroup data (optional). (Not recommended)


iReport: Creating ReportSelect report layout. (Tabular Layout is recommended)


iReport: Creating ReportClick Finish.


iReport: Creating ReportYou may change the report further. E.g., double-click here to change the report title.


iReport: Creating ReportTo preview the report, click here.


Report sectionBrief descriptions of the most important sections for designing a report are given below. title queryString pageHeader columnHeader detail columnFooter pageFooter Summary


Report section (Contd)>> title As the name implies, this section contains the report title and appears at the top of the report. >> queryString This section defines the SQL query to obtain data for report filling process. This section of the XML template does not contain a height or a width.

>> pageHeaderThis section appears at the top of each page in the report and can be used to display information such as dates, page numbers, etc. >> columnHeader This section is used to display the information as heading for each column.20

Report section (Contd)>> detail This is the body of the report. It contains data from the data repository. >>columnFooter This section goes at the bottom of each column and can be used to display information such as the total number of records.

>> pageFooterThis section appears at the bottom of each page in the report. >> Summary

This section contains the information that will be displayed at the end of the report.


iReport: Creating ReportThere are 2 files in the Reports folder. The jasper file allows you to change the report and the jrxml file is the report itself. Next, we are going to call the report (ProductList.jrxml) using Netbeans.


JasperReportsSince JasperReports is not part of the standard Java API, we have to integrate the source files, bits, and documentation into NetBeans so that Netbeans can compile JasperReport class library or API. First, unzip jasperreports-3.5.2-project.zip to anywhere you like. Start Netbeans. Click Tools, then click Libraries.


Jasper ReportClick New Library and enter JasperReports-3.5.2 here.


Jasper ReportClick New Library and enter JasperReports-3.5.2 here.


Jasper ReportIn the Classpath tab, add the jasperreports-1.2.5.jar file from the JasperReports dist directory and add the following 5 files from the lib directory:

commons-beanutils-1.8.0.jar!/commons-collections-2.1.jar!/ commons-digester-1.7.jar!/ commons-javaflow-20060411.jar!/ commons-logging-1.0.2.jar!/ In the Sources tab, add the JasperReports src directory. Click OK to close Library Manager. You have successfully integrated the JaspertReport Compilation into Netbeans.26

Jasper Report


Defining the JasperReport Templates File ExtensionJasperReports report templates are simply XML files with the file extension .jrxml. In the following procedure, you define this file extension in NetBeans, which then recognizes .jrxml files as XML files. Click Tools, then click Options.


Defining the JasperReport Templates File ExtensionClick Miscellaneous, then click Files tab. Click New and enter jrxml.


Defining the JasperReport Templates File ExtensionClick here and select XML Files (text/xml). Then, click OK.


Adding JasperReport to a project1 2 3 Right-click project node and choose Properties from the context menu. The Project Properties dialog box is displayed. In the Categories list, select Libraries and click Add Library. The Add Library dialog box is displayed. Select JasperReports-3.5.2 under Libraries and click Add Library. See the figure on next slide. Click OK to close the Project Properties dialog box. By doing so, your project will be able to use all the class library available in JasperReport to generate report.

4 5


Adding JasperReport to a project


Create button and enter the code to call JasperReport For illustration purpose, we will now create a new JFrame and add in a button on it.


Codes in the View Product List buttonbtnProductList.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { String reportSource = "Reports/ProductList.jrxml"; Map params = new HashMap(); try { Connection con = DriverManager.getConnection("jdbc:odbc:Inventory"); JasperReport jasperReport = JasperCompileManager.compileReport(reportSource);

JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, params, con);JasperViewer.viewReport(jasperPrint, false); } catch (JRException jrex) { JOptionPane.showMessageDialog(null, "Error in generating report!"); } catch(Exception ex) {34

JOptionPane.showMessageDialog(null, "Unable to generate report!");

Code in button | Fix ImportsThe NetBeans Java editor reports many code errors. As a resolution, import the proper classes with the Fix Imports operation. To correct the code error: 1. Either right-click in the editor and choose Fix Imports from the context menu or,

2. with the focus in the editor, type Alt-Shift-F.NetBeans then inserts all the necessary import statements into your class and the error messages will disappear.


Explanation of JasperFrameworkJasperCompileManager.compileReport The compileReport method takes the JasperReports XML template and compiles it into byte code format (a JasperReports instance), which you can serialize to disk as a .jasper file. You can reuse the compiled templates to generate reports without compiling them unless you've changed the template source. JasperReport jasperReport = JasperCompileManager.compileReport(reportSource);


Explanation of JasperFrameworkJasperFillManager.fillReport The fillReport method takes the compiled report (the JasperReports instance), a few user-defined custom parameters, and a JDBC datasource (the DB connection), In this simple product listing report example, the method fills in jasperReport object and connection.


Explanation of JasperFrameworkJasperPrint jasperPrint = JasperFillManager.fillReport( jasperReport, params, conn);

JasperReport object Connection object Parameter to pass into the report, for SQL or dynamic text content


Explanation of JasperFrameworkJasperExportManager.viewReport The viewReport method displays the generated report in the built-in JasperRepor

Popular Tags:

Click here to load reader

Embed Size (px)