英文:
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:
<![CDATA[SELECT * FROM users where city = (这是我想从Java代码传递作为参数生成报告的值)]]>
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.
<subDataset name="userDataset" uuid="b277feac-f289-4d08-83ad-06eb2f992cb7">
<property name="com.jaspersoft.studio.data.sql.tables" value=""/>
<property name="com.jaspersoft.studio.data.defaultdataadapter" value="userAdapater"/>
<queryString language="SQL">
<![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)]]>
答案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:
<parameter name="cityParam" class="java.lang.String"/>
and in your java code, add this map :
String cityName = "Washington"; // The value you want to pass as the city name
Map<String, Object> map = new HashMap<>();
map.put("cityParam", cityName);
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, map, yourDataSource);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论