2

I have three different queries and want to run them as per value get in jasper report.Queries are written in Jrxml file itself.How I can run different queries based on different dynamic values.

Like (It is just a sudo code)

 If($(a) == "Germany")

   run query 1
 If($(a) == "India")
   run query 2

Any help would be highly appreciated.

Innovation
  • 1,514
  • 17
  • 32

2 Answers2

3

Define parameter $P{a} in mainReport.jrxml

Make subreports subreport1.jrxml (query1), subreport2.jrxml (query2)

Put subreport1 and subreport2 into Title band of mainReport.jrxml

Use PrintWhenExpression in mainReport for properties of subreports (Window->Properties)

Set PrintWhenExpression in mainReport for subreport1: $P{a}.equals("Germany")
Set PrintWhenExpression in mainReport for subreport2: $P{a}.equals("India")

Pass $P{a} into mainReport from your application

sanBez
  • 933
  • 1
  • 8
  • 18
  • 1
    I have a small doubt. Does this signify that for N such queries to be executed we have to create new sub reports ??.. – AngelsandDemons Feb 28 '14 at 06:33
  • Another way is using subdataset. http://stackoverflow.com/questions/7482412/multiple-queries-in-a-single-jasper-document But subdataset have some limitations, AFAIR – sanBez Feb 28 '14 at 06:52
2

You can try using DynamicReports. This is a library based on JasperReports, and let's you construct your report directly from you java code. Here is an axample Dynamic Reports There is a .setDataSource() method which can take a string + a connection objects. Based on some little logic you can set the query there. .setDataSource() is overloaded, and it can even take a ResultSet object. So you can even use a Statement/PreparedStatement object passing different queries and values, and then passing their result as a source for the report. Hope it helps!

P.S: The example uses maven dependencies, but you can also download the .jar and add it to your classpath to make it work

Endrik
  • 2,238
  • 3
  • 19
  • 33