Jasper Report - Java
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.
* JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);
*
*/
This Articles was posted on 3:58 AMThursday, March 13, 2008
|
Labels:
JasperReport,
Java,
Tech
|
0 comments:
Post a Comment