Sending Parameters to JasperReport

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 < parameter name = "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[
    3 SELECT * FROM product WHERE itemName = $P{ itemName } ORDER BY inventoryDate
    4 ]]>
    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[
    3 SELECT * 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

Ads by Google
Java Reporting Tools
Wow your boss with easy-to-use reporting software. Only $60/year!
www.myeclipseide.com



25 Responses

  1. 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


  2. this was really very helpful. i was stuck for nearly two hours to figure out my error.
    thanks a lot!


  3. 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


  4. Hi roshan,

    I am really glad that you found the blog helpfull.

    Regards,
    Suad


  5. Hi, that was helpful. I was stuck with how to pass chunks of SQL as parameters and this really saves my day. :) Much appreciated.


  6. Thanx frnd


  7. Hello,
    I am using subreport with jasperreport.but the following error can occur:

    “The supplied java.sql.Connection object is null.”

    Thanks


  8. 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.


  9. 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


  10. 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.


  11. 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.


  12. 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.


  13. What a difference one ! will make. Really helpful, thanks.


  14. 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


  15. tks alot…. this is that i want…….


  16. Thank you man!, you really saved my life, I was really worried about generating a dynamic query from java, Thank you!!


  17. Yo!
    I found this blog searchin in google
    cheers from POland guys :)


  18. Thank you very much, it was really helpful! The detail about “The ! character is needed” is crucial!
    Peace!


  19. 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


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值