Innovation
Innovation

Reputation: 1524

Jasper Report run different query based on different dynamic values

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.

Upvotes: 2

Views: 2026

Answers (2)

Endrik
Endrik

Reputation: 2258

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

Upvotes: 2

sanBez
sanBez

Reputation: 933

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

Upvotes: 3

Related Questions