I am trying to generate a report using DynamicReports library. The datasource is based on the SQL query having where clause with parameter(s). In the example below, I have set the parameter "orderNo=123456". However, when I run the query, I get an exception as below.
The example shared in the DynamicReports site databasedatasourcereport does not have where clause with parameters. Please suggest what needs to be done to resolve the issue.
Exception:
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]
Source code:
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();
The solution is very simple - the parameter should be declared.
All you need is to add parameter definition:
.addParameter("orderNo", String.class)
The full code will be:
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();