RSS

New to JasperReport : Parameterizing your Report

01 Mar

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!

 
4 Comments

Posted by on March 1, 2010 in English, Programming

 

Tags: , , , , , , , ,

4 responses to “New to JasperReport : Parameterizing your Report

  1. reza

    August 26, 2010 at 11:20 am

    bikin tutorial buat chart dll jg dunk gan!

     
  2. Abdur Rahim

    March 19, 2012 at 6:49 am

    Sir can you help me to find out a solution of sending message via web to mobile , If some web service known to you please reffer me,I want to send the sms from my java swing application

     
  3. jafar

    September 3, 2012 at 7:03 pm

    it`s work,, thanks a lot

     
  4. Kedar

    February 19, 2014 at 6:44 pm

    Thanx a lot 4 dis awesome tutorial

     

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

 
%d bloggers like this: