RSS

Tag Archives: report

New to JasperReport : Parameterizing your Report

In previous article, you’ve learnt how to build your first application using JasperReport. you see that the query fetch all record. We, somehow, never need all records but only several records. In SQL, we can use WHERE clause to add conditional for fetching records. We can do this too with JasperReport.

1. Open your iReport designer
If you follow the tutorial from scratch, open your recent report project about Employee Report. Now, add Parameter into your iReport by right-click to parameters node on the left :

Give it name PARAM_E_ID.

2. Integrate your parameter variable with the query
To integrate your parameter variable, type this query in your query window :

SELECT employee_id AS "E_ID", employee_name AS "E_NAME", salary AS "E_SALARY" FROM employees WHERE employee_id = $P{PARAM_E_ID}

Note : You can dynamically add value to your parameter variable.

3. Preview your report
Click preview button, then you’ll be prompted to insert a value to PARAM_E_ID parameter :

After that, you will see the report will be different :

Copy your .jasper file into your project. Replace it if it’s already exist.

4. Code your application
To fill your parameter as conditional from your Java Application to your report, use java.util.HashMap. In this case, we have 1 parameter, PARAM_E_ID. Then in your hashmap, use this code :

HashMap hm = new HashMap();
hm.put("PARAM_E_ID","E0001");
//and your other parameter

Let’s recall my previous article here and look at the java code part. You will see in the line 52 the code is like this :

JasperPrint jp = JasperFillManager.fillReport(is, null, conn);

To unite the hashmap with your report, replace this part into this :

HashMap hm = new HashMap();
hm.put("PARAM_E_ID","E0001");
JasperPrint jp = JasperFillManager.fillReport(is, hm, conn);

After that, you will see the difference :

Enjoy!

Advertisements
 
4 Comments

Posted by on March 1, 2010 in English, Programming

 

Tags: , , , , , , , ,

New to JasperReport : Build your first impressive application (part 2)

So now, you’ve seen the 2 previous articles. And now, let’s move on the core of JasperReport and the integration of it to the desktop application. I guarantee you that you really need this article because you don’t want your application only capable to do just System.out.println(“Hello Everybody!”); do you?

Let’s get it on!

1. Building your report
You can see in the right panel, there’s a palette panel. If you can’t see it, go to Window -> Palette to activate palette panel. Drag a static text to the Title Section as follows :

Make it look like this

After that, insert column title into similar like this :

Note : You can add additional line, text, etc as you like.

2. Integrate it with Query
Click on the button with this icon :
You will see a query window. Type this query on the text area (note that this is SQL query, not Hibernate query, JPQL, etc.)

SELECT employee_id AS "E_ID", employee_name AS "E_NAME", salary AS "E_SALARY" FROM employees

Let’s analyze the code above. I’m sure that all of you must be familiar with this code. But one thing you have to know that, in JasperReport, ALIAS will be converted automatically into FIELDS. In this case, you have 3 aliases, E_ID, E_NAME, and E_SALARY. All of these will be converted into JasperReport FIELDS Variable, so that you can place these elements in this design.

Next, place these 3 elements into your design (Dont forget, put these in Details Section), just simply like this :

3. Testing your report
Click on preview button in the toolbar. If you work in the right way, you will see display like this :

The report works fine. Remember, whenever you clicked on Preview, the iReport will automatically compile the report from .jrxml into .jasper. The .jasper file is the generated object that you can use it and directly integrate it with the application.

Note : for iReport 3.7.0 user, you have to change the default language from Groovy to Java. Right click on your report name node in the left side, choose properties, and change the language from Groovy to Java.

Let’s move on to Netbeans.

4. Create a new Java Application Project and Insert JasperReport library
Create the java application first. Uncheck the Create Main Class option. After that, add JasperReport library and MySQL JDBC Driver to your project (Right click on the library node of your project, add new library)as follow :

Add New Library

Add JasperReport library & MySQL JDBC Driver on it. You will see like this once you get done :

Next, create 2 package in your Source Packages, 1 package to place your report, the other one to place your main application. Give it name :

Move your .jasper file into your report package.

Insert a main class into your application package.

5. Code your application

Open your main class, and insert this code to your class (i will explain it component per component) :

package id.hans.employee.application;

import java.awt.Dimension;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.swing.JRViewer;

/**
 *
 * @author Hans Kristanto
 */
public class MainApplication {

    Connection conn = null;

    public void initConnection(){
       
        String HOST = "jdbc:mysql://localhost:3306/DATABASE_NAME";
        String USERNAME = "YOUR_MYSQL_USERNAME";
        String PASSWORD = "YOUR_MYSQL_PASSWORD";
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException ex) {
            ex.printStackTrace();
        }

        try {
            conn = DriverManager.getConnection(HOST, USERNAME, PASSWORD);
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
    }

    public void showReport(){
        
        //Path to your .jasper file in your package
        String reportName = "id/hans/employee/report/EmployeeReport.jasper";
        
        //Get a stream to read the file
        InputStream is = this.getClass().getClassLoader().getResourceAsStream(reportName);

        try {
	 //Fill the report with parameter, connection and the stream reader		
            JasperPrint jp = JasperFillManager.fillReport(is, null, conn);
	    
  	 //Viewer for JasperReport
            JRViewer jv = new JRViewer(jp);
	
	 //Insert viewer to a JFrame to make it showable
            JFrame jf = new JFrame();
            jf.getContentPane().add(jv);
            jf.validate();
            jf.setVisible(true);
            jf.setSize(new Dimension(800,600));
            jf.setLocation(300,100);
            jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        } catch (JRException ex) {
            ex.printStackTrace();
        }
    }

    public static void main(String[] args) {

        MainApplication ma = new MainApplication();
        ma.initConnection();
        ma.showReport();
    }

}

If no problem occured, you will see display like this :

Your Report display. Cool isn't it?

Congratulations! you’ve created your first JasperReport application!

 
79 Comments

Posted by on March 1, 2010 in English, Programming

 

Tags: , , , , , , , ,

New to JasperReport : Build your first impressive application (part 1)

I’ve made a previous article about setting up your environment to be a “JasperReport-ready”. You can click here to recall it.

And now, in this article, i will guide you to build your first JasperReport application. For this part, i will split this tutorial into 2 parts.

1. Let’s setup a dummy database. I’m using MySQL 5.0. Suppose we have a database named db_jasper

CREATE TABLE employees (
    employee_id VARCHAR(6) NOT NULL PRIMARY KEY,
    employee_name VARCHAR(30),
    salary INT(6)
);

INSERT INTO employees VALUES ('E0001','Hans Kristanto',5000000);
INSERT INTO employees VALUES ('E0002','Conrad Alvin',3500000);
INSERT INTO employees VALUES ('E0003','Luna Hu',4500000);
INSERT INTO employees VALUES ('E0004','Jina Seo',3650000);
INSERT INTO employees VALUES ('E0005','Ivan Lu',4510000);
INSERT INTO employees VALUES ('E0006','Dennis Ewing',500000);

2. Start your iReport designer.

iReport designer main window

3. Create a new report template
Click on File -> New, then choose any template paper. For example, Blank A4 (in iReport 3.7.0). For iReport 3.7.0 below, just follow the wizard as usual.
NOTES : For a while, never use Report Wizard! You have to do it manually.
Give it name and directory to be saved. Click on finish.

New Report

4. After that, you will see window like this

Report Designer

If you to the report designer, there are 7 sections. I will explain it one by one :

  • Title : is section for placing title document. Title document will only appear on the first page of document.
  • Page Header : is section for placing page header, for example, page number, title, etc. This header will appear in every page of report but not in the first page.
  • Column Header : is section for placing column header for detail title, for example, Employee Name, Salary. You will see the different in the real application.
  • Details : is section where you place all data. Detail section will iterate as much as fetched data. Again, you will see how it works in the real application.
  • Column Footer : similar with Column Header, but placed in the foot.
  • Page Footer : similar with Page Header, but placed in the foot.
  • Summary : is section where you put all summarize. For example, summary of total employees, total profits/loss, etc.

You have to understand these 7 sections first before we continue to the next step. Already understand? let’s go ahead!

5. Create a datasource
in the toolbar, you will see an database icon with a plug in front of it. . You will see a new window appear :

Click on New button on the right side, then follow the wizard. My suggestion :
– use Database JDBC Connection.
– give it name afterward.
– use com.mysql.jdbc.Driver as the JDBC Driver.
– type your database name on JDBC URL e.g. : jdbc:mysql://localhost/db_jasper
– type your username and password.

After that, you will see the drop down next to the database icon will be set into our new datasource :

New DataSource

Everything is done for setting up. Let’s continue for the next core tutorial on the part 2.

 
56 Comments

Posted by on February 27, 2010 in English, Programming

 

Tags: , , , , , , , ,

New to JasperReport : Make it one for all and all for one

Reporting is a critical element that almost every application, either it’s desktop or web based should have. Almost all application developer focused on how to make strong reliable application, but they somewhat forget about this section. Today i will make a basic tutorial for an application that could be integrated into reporting engine, in this case, i will use JasperReport + Java.

Before we start to the core of this tutorial, i will show you how to make your PC/Laptop a “JasperReport-Ready”. Things that you should have to prepare :

1. Netbeans IDE
Netbeans is an integrated development environment (IDE) for Java, PHP, C/C++, Ruby, Python. You can download the latest release here

2. iReport Designer
iReport is a JasperReport designer. With this, you can build your preferable report, design it as your favourite, and integrate it into database. you can download the latest release here

3. JasperReport library
JasperReport is the core engine of reporting library. You can download the library here (need authentication). The dependencies libraries are :

After you’ve done downloaded such many things, let’s make your PC “JasperReport-ready”. Follow these steps :

1. Install Netbeans IDE latest version
2. Install iReport designer
3. Once you’ve installed these 2, follow these special steps :

  • Open your netbeans IDE
  • Click Tools->Libraries as follows :
  • nb-library

    Netbeans IDE

  • A new Library Manager window will appear. Click on New Library.. button
  • Type JasperReport on Library Name (you can use any other name as you like) :

    new-lib

    New Library

  • After that, you will back to Library Manager window. Choose your new jasper report library on Libraries list. Click on Add Jar/Folder button on the right side. Add JasperReport library and all dependencies library. Once it done, you will see a window like this :

    Library Manager

  • Click OK, then your IDE is ready for JasperReport development!

Have Fun!

continue to the next step

 
9 Comments

Posted by on February 27, 2010 in English, Programming

 

Tags: , , , , , , , ,