chapter6 part2 - reporting
Post on 26-Oct-2014
78 Views
Preview:
TRANSCRIPT
JasperReports 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.
JasperReports
6-2
JasperReport 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.
JasperReports
6-3
JasperReport 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.
JasperReports
6-4
iReport
6-5
To use Jasper reporting, you need
1. Netbeans IDE 6.0 or newer
2. JasperReports-3.5.2 class library
3. iReport 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.
Pre-requisite
6-6
You 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 JasperReport
6-7
You 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.
Download iReport
6-8
9
iReport: Installation and Setup
Install iReport and run it.
Click Step 1 to setup database connection.
Then, click Next.
10
iReport: Installation and Setup
Enter your database name here.
Select JDBC-ODBC Bridge (sun.jdbc.odbc. JdbcOdbcDriver).
Enter your database name here.
Click Test.
If connection test successful, click Save.
11
iReport: Creating Report
Before 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 iReport’s Welcome Window and enter your report name here. Browse for Reports folder in your project folder. Click Next.
12
iReport: Creating Report
Select the database connection you have created in Step 1.
Enter your SQL statement.
Click Next.
13
iReport: Creating Report
Fields may be added one by one or all together.
14
iReport: Creating Report
Group data (optional). (Not recommended)
15
iReport: Creating Report
Select report layout. (Tabular Layout is recommended)
16
iReport: Creating Report
Click Finish.
17
iReport: Creating Report
You may change the report further. E.g., double-click here to change the report title.
18
iReport: Creating Report
To preview the report, click here.
19
Report sectionBrief descriptions of the most important sections for designing a report are given below.
title
queryString
pageHeader
columnHeader
detail
columnFooter
pageFooter
Summary
20
Report section (Cont’d)>> 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.
>> pageHeader
This 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.
21
Report section (Cont’d)>> 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.
>> pageFooter
This 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.
22
iReport: Creating Report
There 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.
23
JasperReports
Since 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.
24
Jasper Report
Click New Library and enter JasperReports-3.5.2 here.
25
Jasper Report
Click New Library and enter JasperReports-3.5.2 here.
26
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.
27
Jasper Report
28
Defining the JasperReport Templates’ File Extension
JasperReports 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.
29
Defining the JasperReport Templates’ File Extension
Click Miscellaneous, then click Files tab. Click New and enter jrxml.
30
Defining the JasperReport Templates’ File Extension
Click here and select XML Files (text/xml). Then, click OK.
31
Adding JasperReport to a project
By doing so, your project will be able to use all the class library available in JasperReport to generate report.
1 Right-click <yourProjectName> project node and choose Properties from the context menu.
2 The Project Properties dialog box is displayed.
3 In the Categories list, select Libraries and click Add Library. The Add Library dialog box is displayed.
4 Select JasperReports-3.5.2 under Libraries and click Add Library. See the figure on next slide.
5 Click OK to close the Project Properties dialog box.
32
Adding JasperReport to a project
33
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.
34
•Codes in the View Product List button
btnProductList.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
String reportSource = "Reports/ProductList.jrxml";
Map<String, String> params = new HashMap<String, String>();
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) {
JOptionPane.showMessageDialog(null, "Unable to generate report!");
35
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.
36
Explanation of JasperFramework
JasperCompileManager.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);
37
Explanation of JasperFramework
JasperFillManager.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.
38
Explanation of JasperFramework
JasperPrint jasperPrint =
JasperFillManager.fillReport( jasperReport, params, conn);
JasperReport object
Parameter to pass into the report, for SQL or dynamic text content
Connection object
39
JasperExportManager.viewReport
The viewReport method displays the generated report in the built-in JasperReport Viewer.
This is useful feature that enables you to quickly see how a report looks and you may print report from it directly.
The 2nd parameter is a boolean data type to indicate the isExitOnClose state.
If set to true, the whole application is closed when the JasperViewer is closed.
False then otherwise. ( we always set it to false)
Explanation of JasperFramework
JasperViewer.viewReport(jasperPrint, false);
top related