Home > Database > Mysql Tutorial > How to Pass Parameters to WHERE and ORDER BY Clauses in JasperReports Queries?

How to Pass Parameters to WHERE and ORDER BY Clauses in JasperReports Queries?

DDD
Release: 2025-01-18 06:37:09
Original
678 people have browsed it

How to Pass Parameters to WHERE and ORDER BY Clauses in JasperReports Queries?

When building queries in JasperReports, you may need to pass dynamic parameters from the user. For this purpose, there are two syntax expressions for parameter references: $P{} and $P!{}.

$P{} Grammar

The

$P{} syntax is mainly used to set input parameter values ​​in the WHERE clause. It uses a "smart" replacement algorithm similar to java.sql.PreparedStatement. For example, java.lang.String parameters will be replaced with quoted values, and java.lang.Integer parameters will be replaced with numeric values.

For example, consider the following parameters:

参数名称 参数类型 参数值
eventName java.lang.String Olympic Games
eventType java.lang.Integer 2

Original expression (to be replaced):

<code class="language-sql">SELECT startDate, endDate, rating
FROM events
WHERE name=$P{eventName} AND type=$P{eventType}</code>
Copy after login

The result will be:

<code class="language-sql">SELECT startDate, endDate, rating
FROM events
WHERE name='Olympic Games' AND type=2</code>
Copy after login

$P!{} Grammar

The

$P!{} syntax is mainly used for simple substitutions. It replaces parameter values ​​verbatim, without any special treatment.

For example, consider the following parameters:

参数名称 参数类型 参数值
tableName java.lang.String events
eventName java.lang.String Olympic Games

Original expression (to be replaced):

<code class="language-sql">SELECT startDate, endDate, rating
FROM $P!{tableName}
WHERE name='$P!{eventName}'</code>
Copy after login

The result will be:

<code class="language-sql">SELECT startDate, endDate, rating
FROM events
WHERE name='Olympic Games'</code>
Copy after login

Usage in your case

According to your question, you need to set WHERE and ORDER BY clause dynamically, you can use the following expression:

<code class="language-sql">SELECT name, phone, email
FROM company
WHERE $P!{clause} = $P{key}
ORDER BY $P!{order}</code>
Copy after login

where $P{key} is a java.lang.String parameter. If the $P!{clause} value requires a quoted value, you can use the following expression instead:

<code class="language-sql">SELECT name, phone, email
FROM company
WHERE $P!{clause} = '$P!{key}'
ORDER BY $P!{order}</code>
Copy after login

The above is the detailed content of How to Pass Parameters to WHERE and ORDER BY Clauses in JasperReports Queries?. 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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template