Home > Database > Mysql Tutorial > How to Pass a Dynamic SQL 'IN' Parameter List in JasperReports?

How to Pass a Dynamic SQL 'IN' Parameter List in JasperReports?

Mary-Kate Olsen
Release: 2024-12-31 08:15:10
Original
569 people have browsed it

How to Pass a Dynamic SQL

Passing SQL "IN" Parameter List in JasperReports

When generating JasperReports with SQL queries using the "IN" predicate, dynamic parameter assignment can be achieved through Java programming. Let's explore how to set the value of the "roles" parameter dynamically.

The query in question:

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

Parameter Assignment in Java

JasperReports provides a special variable, $X, for setting parameters dynamically. To assign a list of values to the "roles" parameter, use the following syntax:

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

Here's an example of how to set the parameter value programmatically in Java:

// Get a JasperReports instance
JasperPrint jasperPrint = jasperReport.fill(parametersMap, dataSource);

// Create a new parameter list
java.util.List<JRParameter> updatedParameters = new ArrayList<>();

// Add the "roles" parameter with the updated value
updatedParameters.add(new JRParameter("roles", ArrayList.class, roles));

// Set the parameter list
jasperPrint.setParameters(updatedParameters);
Copy after login

Additional Notes

  • The roles list should be of type ArrayList.
  • Refer to the JasperReports documentation for more details on parameter handling.

The above is the detailed content of How to Pass a Dynamic SQL 'IN' Parameter List in JasperReports?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template