DynamicReports 未找到查询参数错误

Query parameter not found error with DynamicReports

我正在尝试使用 DynamicReports 库生成报告。数据源基于具有带参数的 where 子句的 SQL 查询。在下面的示例中,我设置了参数 "orderNo=123456"。但是,当我 运行 查询时,出现如下异常。

DynamicReports 站点 databasedatasourcereport 中共享的示例没有带参数的 where 子句。请建议需要做什么来解决这个问题。

异常:

Query parameter not found : orderNo
at net.sf.jasperreports.engine.design.JRAbstractCompiler.verifyDesign(JRAbstractCompiler.java:280) ~[jasperreports-6.8.0.jar:6.8.0-2ed8dfabb690ff337a5797129f2cd92902b0c87b]
at net.sf.jasperreports.engine.design.JRAbstractCompiler.compileReport(JRAbstractCompiler.java:152) ~[jasperreports-6.8.0.jar:6.8.0-2ed8dfabb690ff337a5797129f2cd92902b0c87b]
at net.sf.jasperreports.engine.JasperCompileManager.compile(JasperCompileManager.java:358) ~[jasperreports-6.8.0.jar:6.8.0-2ed8dfabb690ff337a5797129f2cd92902b0c87b]
at net.sf.jasperreports.engine.JasperCompileManager.compileReport(JasperCompileManager.java:618) ~[jasperreports-6.8.0.jar:6.8.0-2ed8dfabb690ff337a5797129f2cd92902b0c87b]
at net.sf.dynamicreports.jasper.builder.JasperReportBuilder.toJasperReport(JasperReportBuilder.java:291) ~[dynamicreports-core-5.1.0.jar:na]

源代码:

JasperReportBuilder jasperReportBuilder = report().columns(col.column("Order No", "orderNo", type.stringType()))
        .columns(col.column("Order Amount", "orderAmount", type.bigDecimalType()))
        .setQuery("select orderNo, orderAmount from orders where orderNo=$P{orderNo}",
                QueryLanguage.SQL)
        .setConnection(connection).setDefaultFont(stl.font(DEFAULT_FONT, false, false, 12))
        .setParameter("orderNo", "123456")
        .pageFooter(Components.pageXofY());
JasperPrint jp = jasperReportBuilder.toJasperPrint();

解决方法很简单——应该声明参数。

你只需要添加参数定义:

 .addParameter("orderNo", String.class)

完整代码为:

JasperReportBuilder jasperReportBuilder = report()
    .columns(col.column("Order No", "orderNo", type.stringType()),
            col.column("Order Amount", "orderAmount", type.bigDecimalType()))
    .setQuery("SELECT orderNo, orderAmount FROM orders WHERE orderNo=$P{orderNo}",
            QueryLanguage.SQL)
    .setConnection(connection).setDefaultFont(stl.font(DEFAULT_FONT, false, false, 12))
    .addParameter("orderNo", String.class) // Parameter declaration (of String type)
    .setParameter("orderNo", "123456")
    .pageFooter(Components.pageXofY())
    .show();