Thursday, May 1, 2008

iReport Tutorial (part 4) - Calling Report With Parameter

Sometimes we need to pass one or more parameters to our report. For example, we may need to generate a report for some items with a spesific category or price. Based on our previous tutorial, we're going to make a report to view the contacts list with a certain address. For this tutorial, you have to create a new report.

Adding New Parameter

You need to specify address as a parameter, so click on View menu, then click Parameters. Click New to add new parameter. Give a name for your new parameter and specify its data type. For example, I give the name PRM_ADDRESS for this parameter and its data type is String. After that, give a check mark on Use as a prompt option. You can see the picture below for a clearer information.

Create Report Query

After you create a new parameter, you have to create report query based on that new parameter. How we do this? Click on Data menu, then click on Report Query. In the query text box, type this SQL command: SELECT * FROM contact WHERE address=$P{PRM_ADDRESS}.

As you can see the $P{PRM_ADDRESS} represents the parameter that you've made before.

After that, you have to design the report. The report's layout is up to you now. I will not talk about how to design the report here because I have talked about it in the previous tutorial.

Write Your Code

Let's write the code! Same like the previous tutorial, I'll give you all the code so you can copy-paste and modify it if necessary.



Execute It!

Final step is execute your program. Don't forget to set the CLASSPATH. If you forget how to do that, you can see the previous tutorial right before this one.

13 comments:

syedfadzil said...

hi, this tutorial helps me a lot when post 1 param..
but i dont understand how do we post more than 1 param?

Bogeyman said...

Try this:

Map<String, Object> param=new HashMap<String, Object>();
param.put("param1", txtParameter1.getText()); param.put("param2", txtParameter2.getText());


Where txtParameter1 and txtParameter2 should be the existing text field.

System Admin said...

Hi friend,
This tutorial is very helpful for my requirement, any doubt in future i will surely post you.

Abdul said...

Hi,
Would like to know if there is a way to populate the dropdown list (for selecting the passed parameters, pulling these by querying the DB)..?

Bogeyman said...

Hi Abdul,

You can query the table which you want to add its records to your ComboBox. Then, you can loop through the result and use the addItem() method (you can pass a string as its parameter). This method is available in JComboBox class.

wempi said...

help me please, how to calling report from URL with parameter.

ex.
"http://localhost:8080/report.jasper"

Anonymous said...

Hello Bogeyman. Can you please tell me, how to pass a parameter to a jasper report, where the parameter should be like: WHERE id IN ($P{ParameterList}) ? /Jakob

Philip said...

Thank you, thank you, thank you. I have been looking EVERYWHERE on how to map parameters. You have really helped!!!

Thanks,
Philip

Anonymous said...

Indeed this is a valuable code.

Anonymous said...

Hai, my name is fitri. I allways use ireport for adempiere with windows os. But when i want to run it in linux, my report null. can u help me?

LuvMyth said...

I guess this is best tutorial I've found after looking all over the net!

Thanks a ton for helping out people!!!

Amit said...

hello,
I m Amit.
I m new in ireport. I have created .jrxml & .jasper files. I working on my web app. project. On my jsp page their are 2 drop down menu say "start date" & "end date" & one button "Get Report", when i clicked on this button is should give the report contains record between start & end date.
So plz help as early as possible.
Thank You
Email-ID - amit9_s@yahoo.com

nanz said...

can you give tutorial for creating report with jtable.