In many different forums, I have seen posts regarding how to send parameters to JasperReport. If you are still looking for an answer, you can definitely find it over here.
I am assuming that you are familiar with JasperReport and the JRXML template.
JasperReport allows you to pass parameters to the report during runtime. In order to do so you need to:
- Declare the parameter in your report template (jrxml) as follow
1<parametername="itemName"class="java.lang.String"/>You can set a default value for the parameter in case it is not been passed. In order to do so, you can use the child element defaultValueExpression.
- You can then refer to the parameter that you have declared in your template using the syntax “$P{PARAMETER NAME}”
- To refer to the parameter declared earlier in a text field use the follow syntax
1<textFieldExpression>2$P{ itemName }3</textFieldExpression>The text field will display the value of this parameter when the report is filled.
- To pass the parameter declared earlier to a query, use the following syntax
1<queryString>2<![CDATA[3SELECT * FROM product WHERE itemName = $P{ itemName } ORDER BY inventoryDate4]]>5</queryString> - To dynamically modify portions of the SQL query or to pass the entire SQL query as a parameter the syntax differs a little. BE CAREFUL. The ! character is needed. See the following example
1<queryString>2<![CDATA[3SELECT * FROM $P!{MyTable} ORDER BY $P!{OrderByClause}4]]>5</queryString>
The parameters here are used to pass the table name in the FROM clause and the column names in the ORDER BY clause. Note that you cannot use the normal parameters (i.e. $P{}) to dynamically modify your query.
Please note that the reporting engine first deals with the $P!{} parameter references by using their values to obtain the final form of the SQL query, and only after that it transforms the rest of the $P{} parameters.
Reference: The Definitive Guide to JasperReports by Teodor Danciu and Lucian Chirita
Wow your boss with easy-to-use reporting software. Only $60/year!
www.myeclipseide.com
本文详细介绍了如何在JasperReport中定义和使用参数,包括在模板中声明参数、设置默认值、引用参数的方法,以及如何在SQL查询中动态修改部分或传递整个查询作为参数。
2725







hi,
thank u very mush for this lession.and i want to more about how can pass parameter to jasper report from java code.please tell me how can do that.
thanks
Mohan
this was really very helpful. i was stuck for nearly two hours to figure out my error.
thanks a lot!
Hi MOhan,
You can do so using one of the overloaded fillReport method. In the above example we would need to use the folowing metod
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parametersMap, jdbcConnection);
where the keys in the parametersMap shoud be excatly the same as the parameters defined in your report template.
Regards,
Suad
Hi roshan,
I am really glad that you found the blog helpfull.
Regards,
Suad
Hi, that was helpful. I was stuck with how to pass chunks of SQL as parameters and this really saves my day.
Much appreciated.
Thanx frnd
Hello,
I am using subreport with jasperreport.but the following error can occur:
“The supplied java.sql.Connection object is null.”
Thanks
hi
i create master report and sub report . Design page its working But java run time only master Report only working . sub report not working . how to solve this problem.
Hi,
I really appreciated your blog. But what about embedding a sql query with SET statements in the jrxml file, something like:
set @var=’1′;
select * from table where column=@var;
With this technique I obtain an empty report from jasper report even if I use multiple queries jdbc switch on the jdbc initialization string (with J/connector + mysql).
Can me suggest a solution ?
Thanks.
Aki
Man that helped me a lot. Thanks for your article. Been struglin with those parameters in the SQL query for a while now. None of others articles on this topic didn’t mention the ‘!’ in ‘$P’ markers… This is brilliant. hanks again.
Peace.
Your blog is really helpful. But i don’t know how to pass parameter values to a report from a java program. Pls help me. Thanks
Well passing parameters is just as easy as passing a HashMap with the key value pairs. Its as simple as that. Then reference that key in your JRXML. Thats all.
Thanks lot buddy, I knew how to pass parameters but didn’t know how to use ‘em in place of table name in FROM clause.
Could u plz tell me where did u get this syntax from.
I dont remeber very well but I think my colleague was reading a book on one of the following
1)Apress.The.Definitive.Guide.to.JasperReports
2)Packt.Publishing.JasperReports.for.Java.Developers
You can easily get it using eMule.
Nice to know it helped.
What a difference one ! will make. Really helpful, thanks.
Hi Guys,
has anyone come across on how to Invoke a Java Function from a Jasper Report? I need to be able to fill a temp table using a java function and then display the results in my report. But i also need to pass the Parameters to the java code first.
Let me know if any part of the above query is ambiguous.
Cheers,
Jas
Am not really sure why you would want to do that. However I belive it would be advisable to call the Java function in your code , get the result and put it in a temp table(HashMap or Array) and pass it to JasperReports as a parameter. Again I personally dont prefer sending a lot of data like that.
Anyways, I know this is not the answer to you question but just a different line of thinking. Do let me know how it went out.
Regards,
Franklin
tks alot…. this is that i want…….
Am really happy this has saved the lives of quite a few!!
Thank you man!, you really saved my life, I was really worried about generating a dynamic query from java, Thank you!!
Your most welcome. The phenomenal response and benefit, of this one single post, never cease to suprise us. Peace!!
Yo!
I found this blog searchin in google
cheers from POland guys
Thank you very much, it was really helpful! The detail about “The ! character is needed” is crucial!
Peace!
Hey!
Just popping in to leave a thank you – the bit about the exclamation mark being needed to modify fields and tables in SQL statements saved me a lot of work!
Thank you
Wonderful. Nice to hear it helped a lot of people!!
Regards,
Franklin