Now for this session I want to make a post about JasperReport. Before you create a report using Jasper, you must fulfill all of these prerequisite:
After you provide all the requirement, now let start to make a simple report using iReport.
This is the first step before we create a report, you must provide a connection to your database. How to create a connection in iReport?
We will use Report Wizard for creating a simple report.
SELECT * FROM employees
Now how to make a condition in your query?
SELECT * FROM employees WHERE officeCode = $P{p_officeCode} AND jobTitle = $P{p_jobTitle}
Now we will execute the report(jasper file) and convert into pdf file. I will create two folder in my home directory. I will name them “input” and “output“.
/*
File Name : DatabaseConnector.java
Author : Angga Lingga
*/
import java.sql.Connection;
import java.sql.DriverManager;
public class DatabaseConnector {
private DatabaseConnector() {}
public static Connection getConnection() {
Connection connection = null;
if (connection != null)
return connection;
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
connection = DriverManager.getConnection("jdbc:mysql://localhost/classicmodels", "root", "rootpasswd");
}
catch (Exception e) {
e.printStackTrace();
}
return connection;
}
}
/*
File Name : SimpleReport.java
Author : Angga Lingga
*/
import java.sql.Connection;
import java.util.HashMap;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRExporter;
import net.sf.jasperreports.engine.JRExporterParameter;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
public class SimpleReport {
public static void main(String[] args) {
Connection connection = null;
String jasperFile = "./input/employee.jasper";
String pdfFile = "./output/employee.pdf";
HashMap<String, Integer> params = new HashMap<String, Integer>();
params.put("p_officeCode", 1);
try {
connection = DatabaseConnector.getConnection();
JasperPrint print = JasperFillManager.fillReport(jasperFile, params, connection);
JRExporter exporter = new net.sf.jasperreports.engine.export.JRPdfExporter();
exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, pdfFile);
exporter.setParameter(JRExporterParameter.JASPER_PRINT, print);
exporter.exportReport();
System.out.println("File: " + pdfFile + " successful created");
}
catch (JRException e) {
e.printStackTrace();
System.exit(1);
}
catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
}
}
This a great post. I have searched some articles in Internet but I didn’t it. Now I have find it your website. Thanks a lot.
Thank you very much for this post. You have explain how to use JasperReport using Java. I really like your explanation about reporting. By the way I have new recommendation website for you about reporting. In this website they include a guide to the facets of reporting software, best practices, the latest news, and of course, info on all the major reporting systems. Please check it out at http://www.reportipedia.com
OpExpert is a unified solution to manage the entire IT operations for any organization, small or big. The functionality includes Enterprise Management, Performance Management, Fault Management, Network Performance Management, Server Performance Management, Virtualization Management. http://www.opexpert.com
@gry planszowe
Thank you for your digging … but I didn’t find it on digg.com