simple jasperreports tutorial

1. create a new Java Desktop Application project in your netbeans

File > New Project > Java > Java Desktop Application

2. click Next to proceed

3. define your project name and application class; my project name for this tutorial is TestingReport

4. define libraries such below:

5. create table ‘jasper_users’

CREATE TABLE `jasper_users` (
 `id` int(11) NOT NULL AUTO_INCREMENT,
 `firstname` varchar(64) DEFAULT NULL,
 `lastname` varchar(64) DEFAULT NULL,
 `address` varchar(64) DEFAULT NULL,
 `email` varchar(64) DEFAULT NULL,
 PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

INSERT INTO `jasper_users` (firstname, lastname, address, email)
VALUES ('John', 'Doe', 'Utah, USA', 'johndoe@email.com');

INSERT INTO `jasper_users` (firstname, lastname, address, email)
VALUES ('Mohd', 'Faizal', 'KL, Malaysia', 'mohdfaizal@email.com');

INSERT INTO `jasper_users` (firstname, lastname, address, email)
VALUES ('Onizuka', 'Sr.', 'Tokyo, Japan', 'onizuka@email.com');

6. Main.java

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.mohdfaizal.jasperdemo;

import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.HashMap;
import java.util.Map;
import org.apache.log4j.Logger;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.view.JasperViewer;

/**
 *
 * @author mohdfaizal
 */
public class Main extends JFrame implements ActionListener {

 private Logger logger = Logger.getLogger(Main.class);
 private JTextField txtFirstname = new JTextField(20);
 private JButton submitButton = new JButton("Submit");

public Main() {
 init();
 }

private void init() {
 Container conn = getContentPane();
 conn.setLayout(new FlowLayout());
 conn.add(new JLabel("Search by First Name: "));
 conn.add(txtFirstname);
 conn.add(submitButton);
 setLocationRelativeTo(null);
 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 submitButton.addActionListener(this);
 pack();
 }

public void actionPerformed(ActionEvent e) {
 if (e.getSource().equals(submitButton)) {

Connection connection = null;
 JasperReport jasperReport = null;
 JasperPrint jasperPrint = null;

 try {
 // get search input
 String parameterName = txtFirstname.getText();
 String reportSource = "src/com/mohdfaizal/template/ReportTemplate.jrxml";
 // set parameters
 Map map = new HashMap();
 map.put("param", parameterName);
 // establish database connection
 Class.forName("com.mysql.jdbc.Driver");
 connection = DriverManager.getConnection("jdbc:mysql://localhost/tutorial", "root", "password");
 // compile report
 jasperReport = (JasperReport) JasperCompileManager.compileReport(reportSource);
 jasperPrint = JasperFillManager.fillReport(jasperReport, map, connection);
 //close connection
 connection.close();
 //view report to UI
 JasperViewer.viewReport(jasperPrint, false);

} catch (Exception ex) {
 // exception handling
 logger.error(ex.getMessage(), ex);
 System.err.println(ex.getMessage());
 }
 }
 }

public static void main(String[] args) {
 new Main().setVisible(true);
 }
}

7. ReportTemplate.jrxml


<?xml version="1.0" encoding="UTF-8"?>
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="ReportTemplate" language="groovy" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20">
 <property name="ireport.zoom" value="1.5"/>
 <property name="ireport.x" value="0"/>
 <property name="ireport.y" value="0"/>
 <parameter name="param" class="java.lang.String"/>
 <queryString>
 <![CDATA[
SELECT
 id,
 firstname,
 lastname,
 address,
 email
FROM
 jasper_users
WHERE
 firstname LIKE $P{param}]]>
 </queryString>
 <field name="id" class="java.lang.Integer"/>
 <field name="firstname" class="java.lang.String"/>
 <field name="lastname" class="java.lang.String"/>
 <field name="address" class="java.lang.String"/>
 <field name="email" class="java.lang.String"/>
 <background>
 <band splitType="Stretch"/>
 </background>
 <title>
 <band height="56" splitType="Stretch">
 <staticText>
 <reportElement x="0" y="0" width="555" height="46"/>
 <textElement textAlignment="Center" verticalAlignment="Middle">
 <font size="22" isBold="true"/>
 </textElement>
 <text><![CDATA[Jasper Reports Demo]]></text>
 </staticText>
 </band>
 </title>
 <pageHeader>
 <band height="13" splitType="Stretch"/>
 </pageHeader>
 <columnHeader>
 <band height="25" splitType="Stretch">
 <staticText>
 <reportElement x="50" y="0" width="100" height="20"/>
 <textElement>
 <font isBold="true"/>
 </textElement>
 <text><![CDATA[First Name]]></text>
 </staticText>
 <staticText>
 <reportElement x="161" y="0" width="100" height="20"/>
 <textElement>
 <font isBold="true"/>
 </textElement>
 <text><![CDATA[Last Name]]></text>
 </staticText>
 <staticText>
 <reportElement x="274" y="0" width="100" height="20"/>
 <textElement>
 <font isBold="true"/>
 </textElement>
 <text><![CDATA[Address]]></text>
 </staticText>
 <staticText>
 <reportElement x="395" y="0" width="149" height="20"/>
 <textElement>
 <font isBold="true"/>
 </textElement>
 <text><![CDATA[E-mail]]></text>
 </staticText>
 <staticText>
 <reportElement x="10" y="0" width="27" height="20"/>
 <textElement>
 <font isBold="true"/>
 </textElement>
 <text><![CDATA[ID]]></text>
 </staticText>
 </band>
 </columnHeader>
 <detail>
 <band height="25" splitType="Stretch">
 <textField>
 <reportElement x="50" y="0" width="100" height="20"/>
 <textElement/>
 <textFieldExpression class="java.lang.String"><![CDATA[$F{firstname}]]></textFieldExpression>
 </textField>
 <textField>
 <reportElement x="161" y="0" width="100" height="20"/>
 <textElement/>
 <textFieldExpression class="java.lang.String"><![CDATA[$F{lastname}]]></textFieldExpression>
 </textField>
 <textField>
 <reportElement x="274" y="0" width="100" height="20"/>
 <textElement/>
 <textFieldExpression class="java.lang.String"><![CDATA[$F{address}]]></textFieldExpression>
 </textField>
 <textField>
 <reportElement x="395" y="0" width="149" height="20"/>
 <textElement/>
 <textFieldExpression class="java.lang.String"><![CDATA[$F{email}]]></textFieldExpression>
 </textField>
 <textField>
 <reportElement x="10" y="0" width="27" height="20"/>
 <textElement/>
 <textFieldExpression class="java.lang.Integer"><![CDATA[$F{id}]]></textFieldExpression>
 </textField>
 </band>
 </detail>
 <columnFooter>
 <band splitType="Stretch"/>
 </columnFooter>
 <pageFooter>
 <band height="23" splitType="Stretch"/>
 </pageFooter>
 <summary>
 <band splitType="Stretch"/>
 </summary>
</jasperReport>


8. run the application



9. finish. yeay!

Advertisements

5 thoughts on “simple jasperreports tutorial

  1. How did you get rid of the uuid completly? Everytime i remove it, it regenerate itself and causes problem again

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s