My place to Speak!

Information

This article was written on 28 Dec 2009, and is filled under Programming, Software Development.

Creating a Java Report using JasperReport

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:

  1. Java Development Kit. I will use JDK 1.6.
  2. Download iReport for creating your report.
  3. Download Database Connector. I will use JDBC MySQL connector.
  4. Provide a database. I will use database “classicmodels“.
  5. IDE. I will use Eclipse Galileo.

After you provide all the requirement, now let start to make a simple report using iReport.

Create Connection

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?

  • Data > Connections/Data Sources.
  • New > Database JDBC connection > Next
  • Provide name of this connection. I will name it “con_classicmodels“.
  • Provide JDBC Driver: com.mysql.jdbc.Driver.
  • Provide JDBC URL: jdbc:mysql://localhost/classicmodels.
  • Provide user name and password.
  • Save.

Create a Report

We will use Report Wizard for creating a simple report.

  • File > Report Wizard.
  • Use template None.
  • Select Connections/Data Sources used. Select “con_classicmodels“.
  • Insert SQL Query in text field.
    SELECT * FROM employees
  • Next.
  • Select fields will be displayed. I will select all fileds. > Next.
  • Group by empty. > Next.
  • Select layout you want to use. I will use Tabular > classic_landscapeT.xml.
  • Let Get coloumn descriptions from field descriptions default. > Next.
  • Finish.
  • Save your report and name it classicmodels.jrxml.

Now how to make a condition in your query?

  • Data > Report Query.
  • Insert SQL Query in text field.
    SELECT * FROM employees WHERE officeCode = $P{p_officeCode} AND jobTitle = $P{p_jobTitle}
  • Where $P{name} here is parameter, $F{name} means field, and $V{name} indicates variable in iReport.
  • Click OK.
  • Save your Report.
  • Compile your report. Build > Compile. It will compile your jrxml file into jasper file and it will be saved in iReport directory.
  • If you wanto to view your report, Build > Execute(with active connection).

Executing Report

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);
		}
	}
}

4 Comments

  1. Jon Sebastian
    May 26, 2010

    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.

  2. Robert Julianto
    May 30, 2010

    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

  3. opexpert
    December 28, 2009

    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

  4. Angga Lingga
    April 10, 2010

    @gry planszowe
    Thank you for your digging … but I didn’t find it on digg.com

Leave a Reply