Best view in Firefox and Chrome

Jasper Report - Java

Thursday, March 13, 2008
Convert Article to PDFPrint ArticleEmail Article to FrinedBookmark this Article


JasperReports enables you to create almost any kind of Enterprise's applications. But here I'm only writing how to generate reports using jasper report in Java.



/*
* import statements
*/

import net.sf.jasperreports.engine.*;
import net.sf.jasperreports.engine.design.JasperDesign;
import net.sf.jasperreports.engine.xml.JRXmlLoader;

import java.sql.Connection;
import java.util.Map;
import java.util.HashMap;

/*
* Required jar files to run this class:
* 1. jasperreports-1.2.0.jar
* 2. classes12.jar (for Oracle JDBC connection)
* 3. commons-beanutils-1.5.jar
* 4. commons-collections-2.1.jar
* 5. commons-digester-1.7.jar
* 6. commons-logging-1.0.2.jar
*/


public class ReportDriver {
public static String connectMsg="";
public static boolean ErrorState=false;
private static byte[] bytes= null;
private Connection jdbcConnection;

public ReportDriver() {
}
public static void runReport() {
try{
String reportFile = "Jasper/JasperXML/MIS_Theater_Annual_Report.jrxml";
/* path to the .jrxml file which is created from iReport. (Please refeat this post ) */

String databaseName = "pminterfaces"; // database name
String userName = "postgres"; // database username
String password = "zty03pgr"; // database password
String serverName = "175.41.137.22"; // database server password
jdbcConnection = getConnection(userName, password, serverName, databaseName); // connect to the database

JasperDesign jasperDesign = JRXmlLoader.load(reportFile);
JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign); // Set the parameters to the JasperFillManager.fillReport .....
Map mode = new HashMap();
mode.put("CURRENCT_DATE", "2011-04-16");
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, mode, jdbcConnection);
JasperPrintManager.printReport(jasperPrint, false); // get as printed output
JasperExportManager.exportReportToPdfFile(jasperPrint, "Output.pdf"); // save the output as pdf file
bytes = JasperExportManager.exportReportToPdf(jasperPrint); // export to pdf stream
} catch (Exception ex) {
String connectMsg = "Could not create the report " + ex.getMessage() + " " + ex.getLocalizedMessage();
System.out.println(connectMsg);
Error(true, connectMsg);
ex.printStackTrace();
}
}

public static void Error(boolean er,String msg) {
ErrorState=er;
connectMsg=msg;
}

public static byte[] getPDF() {
return bytes;
}

public static Connection getConnection(String userName, String password, String serverName, String dbName) throws SQLException {
Connection conn = null;
Properties connectionProps = new Properties();
connectionProps.put("user", userName);
connectionProps.put("password", password);
//jdbc:postgresql://localhost:5432/MYDATABASE
conn = DriverManager.getConnection("jdbc:postgresql://" + serverName + ":5432/" + dbName, connectionProps);
System.out.println("Connected to database");
return conn;
}

}



JasperReports organizes data retrieved from a data source according to a report-design defined in a JRXML file. In order to fill a report with data, the report-design must be compiled first. The compilation of the JRXML file representing the report-design is performed by the compileReport() method which is exposed by the JasperCompileManager class. Through compilation, the report design is loaded into a report-design object that is then serialized and stored on disk ( JasperReport class). This serialized object is used when the application wants to fill the specified report-design with data. In fact, the compilation of a report-design implies the compilation of all Java expressions defined in the JRXML file representing the report design. Various verifications are made at compilation time, to check the report-design consistency. The result is a ready-to-fill report-design that will be used to generate documents on different sets of data.
For the first time compileation it would take some amount of time. If u have the compiled sources (that is .jasper ) it will work much faster than the pervioue one. If you use the *.jasper file insted of *.jrxml you can comment the following line and direcetelly call the net.sf.jasperreports.engine.JasperPrint line.

/* * JasperDesign jasperDesign = JRXmlLoader.load(reportFile);
* JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);
*
*/
further, if you need to display the report inside a Swing application, you can use the JRViewer component that is shipped with the library and consists of an embeddable and configurable javax.swing.JPanel component. The JasperViewer is a stand-alone Swing application that uses the JRViewer component to display reports in proprietary format (serialized JasperPrint objects) or in XML format.To help with the report design work, JasperReports provides a report design previewer in the JasperDesignViewer class. And if you need to use this jasper report in a web based application you need to create the byte stream (in any form like pdf HTML or xml ) to the client mechine. Here i only mention about how to create the byte stream. passing to the client is up to your knowledge. If any one wants mode details about the JASPER contact me at shauom[at]gmail[dot]com.

Read these Articles :

0 comments:

Why Breeze

This is a new technological rhythm in the web. It blooms like a Breeze in your technological face with more than 75 technoligical atricles and guidences.

Contact Me
Contact Me Send an Email Facebook Account LinkedIn profile Twitter/Shayanth
Share And Save
Social Share with Twitter Facebook Share Add to Google Save on Delicious
Get Updates

The RSS will facilitate you to get the updates from Breeze. Click Here to redirect to the RSS link.

Designed by Posicionamiento Web | Bloggerized by GosuBlogger | Blue Business Blogger | Customized by SHAUOM