如何将一个值传递给SQL查询以生成Jasper报表

huangapple go评论62阅读模式
英文:

How to pass a value to an sql Query to generate a jasper Report

问题

I understand you want the text within the <queryString> element to be translated. Here it is:

&lt;![CDATA[SELECT * FROM users where city = (这是我想从Java代码传递作为参数生成报告的值)]]&gt;

If you have any more text that needs translation, please let me know.

英文:

still a noob here I really need help with something.

I have a jasper report that I want to generate in my java program, which is displaying information retrieved from mysql database.

I have a query like this as an example:

String query = "SELECT * FROM users where city = ?";

In generating a jasper report, when i pass that sql query with a specific value like city= "Washington", it retrieves the information that i want.

But what I would want to do is for that city string value to be passed from the java code as a parameter to my sql query so that it retrieves information based on the city name that I specify in my java code.

Is there a way i can do it, if yes can I have an example please.
Hopefully i have tried to explain in a manner that is understandable

Will appreciate all the help that I can get.

&lt;subDataset name=&quot;userDataset&quot; uuid=&quot;b277feac-f289-4d08-83ad-06eb2f992cb7&quot;&gt;
		&lt;property name=&quot;com.jaspersoft.studio.data.sql.tables&quot; value=&quot;&quot;/&gt;
		&lt;property name=&quot;com.jaspersoft.studio.data.defaultdataadapter&quot; value=&quot;userAdapater&quot;/&gt;
		&lt;queryString language=&quot;SQL&quot;&gt;
			&lt;![CDATA[SELECT * FROM users where city = (THIS IS THE VALUE I WANT TO PASS AS A PARAMETER FROM JAVA JODE THAT WILL GENERATE THE REPORT)]]&gt;

答案1

得分: 0

你可以在你的jrxml文件中定义一个字符串字段,并将其与Java代码中的一个字符串字段进行映射,按照以下步骤进行操作:

在jrxml文件中定义一个String参数:

<parameter name="cityParam" class="java.lang.String"/>

然后在你的Java代码中添加以下映射:

String cityName = "Washington"; // 你想要传递的城市名称的值
Map<String, Object> map = new HashMap<>();
map.put("cityParam", cityName);

JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, map, yourDataSource);

这样做可以将城市名称传递到报表中。

英文:

you can define a string field in to your jrxml file and map it from your java code with a java String field, following these steps:

define a String parameter in the jrxml file:

&lt;parameter name=&quot;cityParam&quot; class=&quot;java.lang.String&quot;/&gt;

and in your java code, add this map :

String cityName = &quot;Washington&quot;; // The value you want to pass as the city name
Map&lt;String, Object&gt; map = new HashMap&lt;&gt;();
map.put(&quot;cityParam&quot;, cityName);

JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, map, yourDataSource);

huangapple
  • 本文由 发表于 2023年7月6日 16:17:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/76626831.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定