Thursday 3 October 2013

Step by step how to add iReport query as parameter

Leave a Comment
Someone ask me how to pass sql statement as parameter in iReport.

So i did a little research and test by myself. There is a one trick to put sql query statement as a parameter in ireport. The purpose to do this is when you want to call the jasper report in your application is either client or the web, you can pass the specific query to your jasper report.

Here is the step:

  1. Create your report
  2. Use Static query for example like "Select * Fom Companies";
  3. Add your field to your report.
  4. Create one parameter let say "sql_input_param" as java.lang.String
  5. Set the default value same like step no 2.(just in case you forgot to pass the parameter, the default value will handle it.

HERE IS THE TRICK

  1. Modify your query to be $P!{sql_input_param} < -- as you can see, there is a exclamation symbol after letter P. Its is used to pass parameter as sql query in ireport query.

Now you can test to pass query statement as parameter in ireport

HashMap<String, Object> parameters = new HashMap<>();
var sql = "SELECT * FROM companies WHERE companies.company_id = " + forms.frm_company.company_id + " order by companies.company_id";
parameters .put('sql_input', sql);

Connection jdbcConnection;
JasperReport jasperReport;
JasperPrint jasperPrint;
JRPdfExporter exporter = new JRPdfExporter();

JasperDesign jasperDesign = JRXmlLoader.load(F);
jasperReport = JasperCompileManager.compileReport(jasperDesign);
jasperPrint = JasperFillManager.fillReport(jasperReport, param, jdbcConnection);
::
::
::
::
now you can pass the query statement as parameter in iReport. 

By
NOTE : – If You have Found this post Helpful, I will appreciate if you can Share it on Facebook, Twitter and Other Social Media Sites. Thanks =)

0 comments:

Post a Comment

Subscribe to our newsletter to get the latest updates to your inbox.

Your email address is safe with us!




Founder of developersnote.com, love programming and help others people. Work as Software Developer. Graduated from UiTM and continue study in Software Engineering at UTMSpace. Follow him on Twitter , or Facebook or .



Powered by Blogger.