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})
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}
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);
Additional Notes
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!