passing a “where condition” to jasperreports expor

2019-09-10 08:55发布

问题:

A simple SQL table, with two attributes and three rows:

reference | name:

0 | first

1 | second

2 | third

I have a Java application, with Swing, showing this through a JList (each row concatenates the two strings, reference and name).

I push a button, I export the entire database successfully, using my "from_ireport.jasper", compiled from iReport. I can compile from code too.

I want to select one or more elements, push the button and get a pdf with just the selected elements. Where and how do I specify the "where condition" to jasper?

I can't find a easy example to look at. Thanks.

回答1:

For solving this issue you can use the JR report's parameters.

For example you can use this query expression:

<queryString>
        <![CDATA[SELECT reference, name FROM table WHERE $P!{whereCondition}]]>
</queryString>

The sample of Java code for passing parameter's value:

Map<String, Object> params = new HashMap<String, Object>();
params.put("whereCondition", "reference > 0");
JasperReport jasperReport = JasperCompileManager.compileReport(reportSource);
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, params, connection);

For more details you can view Query Sample.