0

我正在尝试构建一个足够智能的报告,可以根据某种输入参数稍微修改其 sql 查询。

例如,如果那个特殊的修改参数值为“1”,它会在 select 中添加一个字段并将 group by 子句添加到查询中。

我研究了 java 表达式,但 jrxml 的 queryString 标记似乎不支持它们。还尝试创建一个包含 java 表达式的变量,并在 queryString 标记中使用该变量......这也不起作用!

现在我正在考虑可能有一个包含所有逻辑的存储过程,并且只是让 jrxml 使用修改输入参数调用该存储过程,但是我正在处理的项目似乎没有大量存储proc,所以我想看看在我走这条路之前是否还有其他解决方案。

谢谢你的帮助。


谢谢各位大神帮忙,非常感谢。但是,我找到了另一种解决方法,并将其发布以供参考:here

4

3 回答 3

8

JasperDesign actually lets you modify portions of your jrxml document. So say you have a package "reports" where you store your report built either by hand or by a tool like iReport. As long as your query is defined in the tag <queryString> the following will work allowing you to change the query on the fly:

try {
    String fileName = getClass().getClassLoader().getResource("com/foo/myproject/reports/TestReport.jrxml").getFile();
    File theFile = new File(fileName);
    JasperDesign jasperDesign = JRXmlLoader.load(theFile);

    //Build a new query
    String theQuery = "SLECT * FROM myTable WHERE ...";

    // update the data query
    JRDesignQuery newQuery = new JRDesignQuery();
    newQuery.setText(theQuery);
    jasperDesign.setQuery(newQuery);

    JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);
    Connection conn = MyDatabaseClass.getConnection();
    JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, null, conn);
    JasperViewer.viewReport(jasperPrint);
} catch (Exception ex) {
    String connectMsg = "Could not create the report " + ex.getMessage() + " " + ex.getLocalizedMessage();
    System.out.println(connectMsg);
}

With something like this you can create a member variable of your class that holds the new query and build it with whatever user constrains desired. Then at view time just modify the design.

-Jeff

于 2009-03-27T14:58:01.870 回答
1

JasperDesign helped me to solve the problem of building dynamic query on Jrxml file.

To build the Dynamic SQL, I was using the Squiggle(Google Code) to build the SQL dynamically. Thanks jeff

于 2010-05-27T13:01:32.527 回答
0

我已经使用存储过程完成了它,这些过程对这些东西来说很好。否则,您可能会切换到 Java。只需从数据库中获取数据并根据用户提供的参数对其进行过滤、分组并作为 bean 的集合发送到将进行渲染的 Jasper 报告。

于 2009-03-17T10:26:09.997 回答