Jasper Reports:如何从 Java 程序传递多个 SQL 查询
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4657421/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Jasper Reports: How to pass multiple SQL queries from a Java Program
提问by Abdul
I have constructed a Jasper Report using iReport tool, where in I have registered two data sets, one to polulate the data in a table, and other to display a chart.
我使用 iReport 工具构建了一个 Jasper 报告,其中我注册了两个数据集,一个用于填充表格中的数据,另一个用于显示图表。
The configuration was successful using the tool and when i see the report it gives me proper data. How ever when i try to invoke the queries from a Java program I am lost. How do i go about handling this? I can only pass a single query with the sample source code I have in as my program.
使用该工具配置成功,当我看到报告时,它给了我正确的数据。每当我尝试从 Java 程序调用查询时,我都迷失了方向。我该如何处理?我只能使用作为我的程序的示例源代码传递一个查询。
Sample Source code I am using:
我正在使用的示例源代码:
Connection conn = getConnection("172.16.88.171", "1522", "orcl", "audi", "audi");
System.out.println("Got jdbc connection...");
Statement stmt = conn.createStatement();
ResultSet rset = stmt.executeQuery("SELECT DB_USER, OS_USER, USERHOST, STATEMENT_TYPE, SQL_TEXT FROM DBA_FGA_AUDIT_TRAIL");
InputStream input = new FileInputStream(new File("E:\jasper_reports\sampleADPTemplate_chart.jrxml"));
JasperDesign design = JRXmlLoader.load(input);
JasperReport report = JasperCompileManager.compileReport(design);
JasperPrint print = JasperFillManager.fillReport(report, new HashMap(), conn);
OutputStream output = new FileOutputStream(new File("E:\jasper_reports\JasperReporttoPDF.pdf"));
JasperExportManager.exportReportToPdfStream(print, output);
This only passes a single query; How do i go about passing multiple queries.
这只会传递一个查询;我如何去传递多个查询。
Thanks.
谢谢。
回答by Costis Aivalis
You only need to store your queries entirely inside your iReport jrxml file. In order to make your report flexible, use parameters. This way you allow the user to define the needed values at runtime.
您只需要将查询完全存储在 iReport jrxml 文件中。为了使您的报告灵活,请使用参数。这样您就允许用户在运行时定义所需的值。
Here you can see an example that gets two values from two combo-boxes, adds them to a HashMap and passes the map to iReport. These paramaters, for this example "storeName
" and "actionCode
", are used to specify values for the query which is stored inside the iReport.
在这里您可以看到一个示例,该示例从两个组合框获取两个值,将它们添加到 HashMap 并将映射传递给 iReport。这些参数,例如“ storeName
”和“ actionCode
”,用于指定存储在 iReport 中的查询的值。
You can have multiple queries inside Sub-Reports.
您可以在子报告中进行多个查询。
try {
String shopName = jComboBox1.getSelectedItem().toString();
String actionCode = jComboBox2.getSelectedItem().toString();
HashMap<String, Object> map = new HashMap<String, Object>();
map.put("storeName", shopName);
map.put("actionCode", actionCode);
URL reportFileURL = getClass().getResource("../ireps/AccessCounter.jrxml");
File reportFile = new File(reportFileURL.toURI());
JasperDesign jasperDesign = JRXmlLoader.load(reportFile);
JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, map, con);
JasperViewer jv = new JasperViewer(jasperPrint);
JDialog viewer = new JDialog(this, "Batch Report", true);
viewer.setBounds(jv.getBounds());
viewer.getContentPane().add(jv.getContentPane());
viewer.setResizable(true);
viewer.setIconImage(jv.getIconImage());
viewer.setVisible(true);
} catch (JRException exc) {
System.out.println(exc.getMessage());
} catch (URISyntaxException exs) {
System.out.println(exs.getMessage());
}