This recipe teaches you how to design a simple wrapper class in Java that wraps JasperReports functionality to process and compile an existing JRXML file, fetch application data, and combine the processed JRXML with application data to generate and save your JasperReport in PDF format.
Refer to the installPostgreSQL.txt
file included in the source code download for this chapter to install and run PostgreSQL, which should be up and running before you proceed.
The source code of this chapter also includes a file named copySampleDataIntoPGS.txt
, which helps you create a database named jasperdb9
and copy sample data for this recipe into the database.
You will need Java Development Kit (JDK) version 1.6.X to compile and run the Java application of this recipe. You will find all the necessary downloads and instructions on Sun's official website. After installing JDK, you will also set the JAVA_HOME
environment variable to point to the main folder of your JDK installation (..jdk1.6.X
).
You will be using a JRXML file named MonthlyCustomerReport.jrxml
and two Java files named ConnectionManager.java
and JasperReportsWrapper.java
in this recipe. You will find these three files in the Task1
folder of the source code download of this chapter. In addition, you will also find two .bat
files named compile.bat
and run.bat
and two folders named com
and lib
in the same Task1
folder. Copy all five files along with the com
and lib
folders (with their contents) to the C:JasperReportsCookBookSamples
folder on your PC.
In order to compile JasperReportsWrapper.java
, you will need the following seven JAR files:
You will find all seven JAR files in the ..JaspersoftiReport-nb-3.Xireportmodulesext
folder of your iReport installation.
Although your JasperReportsWrapper.java
will compile with these seven JAR files, your complete application will not run without the JDBC JAR file that contains the driver for PostgreSQL. The name of the JAR file is postgresql-8.2-506.jdbc3.jar
. You can download the PostgreSQL JDBC driver from http://jdbc.postgresql.org/download.html. You can also find this postgresql-8.2-506.jdbc3.jar
file in the ..JaspersoftiReport-nb-3.Xide8modulesext
folder of your iReport installation.
Once you have all the JAR files ready, you will put them in the C:JasperReportsCookBookSampleslib
folder on your PC. The compile.bat
and run.bat
files assume that all JAR files are located in the C:JasperReportsCookBookSampleslib
folder.
The following steps demonstrate how to incrementally design a wrapper Java class to process your JasperReport and generate its PDF.
C:JasperReportsCookBookSamples
folder on your PC into which you copied files from the Task1
folder from the source code for this chapter. compile.bat
file, and a command prompt will appear. It will compile your JasperReportsWrapper.java
file and show the compilation results. After successful compilation, dismiss the command prompt by clicking the Close button on its top-right corner. Now you will see that a JasperReportsWrapper.class
file will appear in the C:JasperReportsCookBookSamplescom
folder. run.bat
file to execute the compiled JasperReportsWrapper
class. You will see that your Java application displays a Connection with database established successfully message on command prompt. Dismiss the command prompt by clicking the Close button. JasperReportsWrapper.java
file from the C:JasperReportsCookBookSamples
folder on your PC in a text editor. The file contains code for your JasperReportsWrapper
class. You will find five methods named connect2DB(), compileJRXMLFile(), fillReport(), saveReportInPDF()
, and main()
in the JasperReportsWrapper
class. The first method named connect2DB()
is ready to be used in the recipe. The connect2DB()
method connects to your PostgreSQL database and returns the connection in the form of a Connection
object. You will incrementally build the rest of the four methods in this recipe to compile, fill, and save your JasperReport. MonthlyCustomerInvoices.jrxml
file. For this purpose, type the following code in the try
block of the compileJRXMLFile()
method in the JasperReportsWrapper.java
file.JasperReport jr = JasperCompileManager.compileReport(jasperXMLFileName); if (jr != null) System.out.println ("Your report compiled successfully"); return jr;
After you have typed the code, the complete compileJRXMLFile()
method will look as follows:
// Compile JasperReports XML file
public JasperReport compileJRXMLFile(String jasperXMLFileName)
{
try
{
JasperReport jr = JasperCompileManager.compileReport(jasperXMLFileName);
if (jr != null)
System.out.println ("Your report compiled successfully");
return jr;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
Save your JasperReportsWrapper class.
compileJRXMLFile()
method at the end of the existing code in the main()
method of your JasperReportsWrapper
class. The call to the compileJRXMLFile()file
will look as follows:JasperReport compiledJasperReport = wrapper.compileJRXMLFile(path2JRXMLFile);
After adding the call, the main()
method will become:
public static void main (String[] args)
{
JasperReportsWrapper wrapper = new JasperReportsWrapper();
Connection connection = wrapper.connect2DB( dbServerAdd, dbServerPort, dbName, dbUser, dbPass);
// Compile JRXML file
JasperReport compiledJasperReport = wrapper.compileJRXMLFile(path2JRXMLFile);
}
Save your JasperReportsWrapper class.
compile.bat
file to compile the JasperReportsWrapper
class. A command prompt will appear. It will compile your application. After successful compilation, dismiss the command prompt by clicking on the Close button. run.bat
file to execute the compiled JasperReportsWrapper.class
file. You will see that your Java application displays Connection with database established successfully and Your report compiled successfully messages. Dismiss the command prompt by clicking the Close button. Connection
object that you saw earlier in step 4 to connect to your PostgreSQL database. Recall from step 4 that connect2DB()
method returned this Connection
object. You will pass the Connection
object to the FillManager
class of JasperReports by typing the following code in the try
block of the fillReport()
method of the JasperReportsWrapper
class:if (connection != null) { JasperPrint jp = JasperFillManager.fillReport(jr, params, con); if (jp != null) System.out.println (" Your report filled with application data"); return jp; }
After typing the code, the fillReport()
method will look as follows:
// Fill data in compiled JasperReports file public JasperPrint fillReport ( JasperReport jr, Map params, Connection con) { try { if (connection != null) { JasperPrint jp = JasperFillManager.fillReport(jr, params, con); if (jp != null) System.out.println (" Your report filled with application data"); return jp; } } catch (Exception e) { e.printStackTrace(); } return null; }
Save your JasperReportsWrapper class.
fillReport()
method at the end of the existing code in the main()
method of your JasperReportsWrapper
class, as shown below:JasperPrint jasperPrint = wrapper.fillReport(compiledJasperReport, null, connection);
After adding the call, the main()
method will become:
public static void main(String[] args)
{
JasperReportsWrapper wrapper = new JasperReportsWrapper();
Connection connection = wrapper.connect2DB( dbServerAdd, dbServerPort, dbName, dbUser, dbPass);
// Compile JRXML file
JasperReport compiledJasperReport = wrapper.compileJRXMLFile(path2JRXMLFile);
// Fill data in compiled JRXML file
JasperPrint jasperPrint = wrapper.fillReport( compiledJasperReport, null, connection);
}
Save your JasperReportsWrapper
class.
compile.bat
file to compile the JasperReportsWrapper
class. A command prompt will appear that will compile your Java file. After successful compilation, dismiss the command prompt by pressing the Close button. run.bat
file to execute the compiled JasperReportsWrapper.class
file. You will see that your Java application displays Connection with database established successfully, Your report compiled successfully, and Your report filled with application data messages. Click the Close button to dismiss the command prompt. saveReportInPDF()
method of the JasperReportsWrapper
class with Java code in its try
block as follows:JasperExportManager.exportReportToPdfFile( jasperPrint, pdfFileName); System.out.println ("Your report successfully saved in PDF");
After typing the code, the saveReportInPDF()
method will look like as follows:
// Compile, fill and save report in JasperReport
public void saveReportInPDF (JasperPrint jasperPrint, String pdfFileName)
{
try
{
JasperExportManager.exportReportToPdfFile( jasperPrint, pdfFileName);
System.out.println (" Your report successfully saved in PDF");
} catch (Exception e) {
e.printStackTrace();
}
}
Save your JasperReportsWrapper
class.
saveReportInPDF()
at the end of the existing code in the main()
method of your JasperReportsWrapper
class as follows:wrapper.saveReportInPDF(jasperPrint, pdfFileName);
After adding the call, the main()
method will become:
public static void main(String[] args)
{
JasperReportsWrapper wrapper = new JasperReportsWrapper();
Connection connection = wrapper.connect2DB( dbServerAdd, dbServerPort, dbName, dbUser, dbPass);
// Compile JRXML file
JasperReport compiledJasperReport= wrapper.compileJRXMLFile();
// Fill data in compiled jasper file
JasperPrint jasperPrint = wrapper.fillReport(
compiledJasperReport, null, connection);
// Save compiled and filled Jasper report in PDF
wrapper.saveReportInPDF(jasperPrint, pdfFileName);
}
compile.bat
file to compile the JasperReportsWrapper
class. A command prompt will appear that will compile your Java file. After successful compilation, click the Close button to dismiss the command prompt. run.bat
file to execute the compiled JasperReportsWrapper.class
file. You will see that your Java application displays Connection with database established successfully, Your report compiled successfully, Your report filled with application data, and Your report successfully saved in PDF messages. Click the Close button to dismiss the command prompt. Now you can browse to the C:JasperReportsCookBookSamples
folder on your PC, where you will find a MonthlyCustomerInvoices.pdf
file. This is the JasperReport, which your JasperReportsWrapper
Java application has generated and saved for you.Your Java application has compiled, generated, and saved your JasperReport in this recipe. In order to do all this, you used a wrapper class named JasperReportsWrapper
, which wraps the functionality of JasperReports.
When you started, the JasperReportsWrapper
class only had the functionality of connecting to a database. You incrementally built it to provide functionality to fetch application data, compile a JRXML file, supply application data to the compiled JRXML file, generate your JasperReport, and finally save it as a PDF file.
In step 5 of the recipe, you used a manager class of JasperReports named JasperCompileManager
to compile your JRXML file. Then, in step 9, you used another manager class named JasperFillManager
to fill the compiled report with application data. Note that the JasperFillManager
class takes both a compiled JasperReport and a database Connection
object. It internally fetches application data and provides the data to the compiled report to generate your actual JasperReport.
Finally, in step 13, you used another manager class named JasperExportManager
to export your filled JasperReport into a PDF file, which you could browse and find in the C:JasperReportsCookBookSamples
folder on your PC.
The following diagram shows the steps that are followed to generate a report:
You can see four steps in the diagram; each step is represented by a box:
18.225.57.126