本文介绍了JRXML中的动态查询字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



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



我已经研究了java表达式,但是在jrxml的queryString标签中似乎并不支持它们。还试图制作一个包含java表达式的变量,并在queryString标签中使用该变量...这也不行!



现在我想到可能有一个存储过程与所有的逻辑,只需要jrxml调用存储过程与修改输入参数,但项目我在工作中似乎没有大量的存储过程,所以我想看看在我走下去之前是否还有其他解决方案。








谢谢你们的帮助,然而,我发现了另一种方式来发表,并张贴了解信息:

解决方案

JasperDesign实际上允许您修改部分jrxml文档。所以说你有一个包报告,你在哪里存储你的报告手工制作,或像iReport这样的工具。只要您的查询在标签< queryString> 中定义,以下操作将允许您即时更改查询:

  try {
String fileName = getClass()。getClassLoader()。getResource(com / foo / myproject / reports / TestReport.jrxml)的GetFile();
文件fileFile = new File(fileName);
JasperDesign jasperDesign = JRXmlLoader.load(theFile);

//构建新查询
String theQuery =SLECT * FROM myTable WHERE ...;

//更新数据查询
JRDesignQuery newQuery = new JRDesignQuery();
newQuery.setText(theQuery);
jasperDesign.setQuery(newQuery);

JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);
连接conn = MyDatabaseClass.getConnection();
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport,null,conn);
JasperViewer.viewReport(jasperPrint);
} catch(Exception ex){
String connectMsg =无法创建报告+ ex.getMessage()++ ex.getLocalizedMessage();
System.out.println(connectMsg);
}

使用这样的东西,您可以创建一个类的成员变量,新查询,并用任何用户约束所需的构建它。然后在视图的时候只需修改设计。



-Jeff


I'm trying to build a report that would be smart enough to modify slightly its sql query based on an input parameter of some sort.

For example if that special modifying parameter value is "1", it adds a field in the select and adds a group by clause to the query.

I've looked into java expressions, but they don't seem to be supported in the queryString tag of the jrxml. Also tried to make a variable containing the java expression and use that variable in the queryString tag... That didn't work either!

Right now I'm thinking of maybe have a stored procedure with all that logic and simply have the jrxml calling that stored procedure with the modifying input parameter, but the project I'm working on doesn't seem to have a whole lot of stored proc, so I'd like to see if there are other solutions before I go down that path.

Thanks for your help.


Thank you guys for your help, much apprieciated. However I found another way to go about it, and posted it for information: here

解决方案

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

这篇关于JRXML中的动态查询字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 01:06