13

I am working on Jasper Reports and my query uses SQL 'IN' predicate.

SELECT customer_name AS NAME, 
       id_customer   AS ID 
  FROM customer 
 WHERE customer_role IN ($P{roles})

Here the role parameter can have 1 or more integer values and will be dynamically decided when generating the jasper report.

Can anybody please help me on how to set the value of 'roles' parameter thru Java program dynamically.

Oscar Pérez
  • 4,377
  • 1
  • 17
  • 36
Priyanka
  • 135
  • 1
  • 2
  • 6

3 Answers3

27

Jasper Report has a special variable $X for that:

select * from customer where $X{IN,customer_role,roles}

should work. See here and here.

Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820
2

The examples linked to in the accepted answer don't come up for me.

An alternative that worked for me is instead of using:

...
WHERE customer_role IN ($P{roles})

I used this:

...
WHERE customer_role IN ($P!{roles})

And for the roles variable I pass in a String containing one or more values, each in single quotes, separated by commas (e.g., '1','2','3').

See here for reference.

Woodchuck
  • 3,869
  • 2
  • 39
  • 70
  • 1
    While this will work it will **not** use prepared statement, hence the application can be targeted by [SQL injection attacks](https://stackoverflow.com/questions/332365/how-does-the-sql-injection-from-the-bobby-tables-xkcd-comic-work) – Petter Friberg Jan 26 '21 at 08:09
  • Petter Friberg, do you have prepared statement usage example with jasper-reports? I believe the accepted answer also does not use prepared statements. – Woodchuck Dec 21 '21 at 14:32
1

To complement @Aaron response, you can configure a list parameter ("roles" in your case) within JasperSoft Studio as follows:

List parameter

mkl
  • 90,588
  • 15
  • 125
  • 265
mikk3l
  • 45
  • 7