October 06, 2009

Jasper Repport

With Jasper Report it has been found that reporting is become easy to desing and integrate with Java application . So far there are many other way to represent a group of data in a organised manner using Jfree chart or iReport or some time may be usiong Open Lazlo. Similarly Jasper Report.... But obviously each has it's own essence and how to make that beat use in any application always comes with profound understanding with technical part .....

Step 1. Get a database connection and Retrive the result for the specific query .
*************************************************************************************

public static ResultSet establishConnection(){
Connection con = null;
Statement stmt = null;
ResultSet rs = null ;

try {
Class.forName("***********").newInstance();
con = DriverManager.getConnection("**********","username", "password");

stmt= con.createStatement();
rs = stmt.executeQuery("SELECT * FROM test.countrylist order by countryName") ;


} catch (Exception e) {
e.printStackTrace();
}

return rs;

}

*************************************************************************************



Step 2.Generate the JRXML file.


step 3.Pass the above with a HashMap in JasperFillManager.fillReport and then generate report.

*************************************************************************************

public static void generateReport(){

int i ;

try {
Date d;
JasperReport jasperReport;
JasperPrint jasperPrint;
ResultSet rs = establishConnection();
HashMap jasperParameter = new HashMap();
JRResultSetDataSource jasperReports = new JRResultSetDataSource(rs);

InputStream input = new FileInputStream(new File("F:/jaspertest.jrxml"));
JasperDesign design = JRXmlLoader.load(input);


jasperReport = JasperCompileManager.compileReport(design);
jasperPrint = JasperFillManager.fillReport(jasperReport,jasperParameter, jasperReports);


JasperExportManager.exportReportToPdfFile(jasperPrint, "F:/sample_report.pdf");
JasperExportManager.exportReportToHtmlFile(jasperPrint, "F:/sample_report.html" );


}
catch (JRException e)
{
e.printStackTrace();
}
catch (Exception e)
{
e.printStackTrace();
}

}

*************************************************************************************

No comments:

Post a Comment