All reports, regardless of how the data is presented, take a path to the report template and a parameter map. The variables are used in all examples that follow:
// Parameters passed into the report.
Map<String, Object> parameters = new HashMap<>();
// Arbitrary parameter passed into the report.
parameters.put("KEY", "Value");
// The compiled report design.
String path = "path/to/template.jasper";
Using a .jrxml file incurs an extra compilation step that isn't necessary in most situations. Unless you've written custom software to change the .jrxml before the report runs (e.g., adding or removing columns dynamically), use the .jasper file as shown in the subsequent examples.
// Establish a database connection.
Connection connection = DriverManager.getConnection(url, username, password);
// Fill the report, get the JasperPrint that can be exported to desired format.
JasperPrint jasperPrint = JasperFillManager.fillReport(
path, parameters, connection);
// Populate this list of beans as per your requirements.
List<Bean> beans = new ArrayList<>();
// Wrap the beans in a beans in a JRBeanCollectionDataSource.
JRBeanCollectionDataSource datasource = new JRBeanCollectionDataSource(beans);
// Fill the report, get the JasperPrint that can be exported to desired format.
JasperPrint jasperPrint = JasperFillManager.fillReport(
path, parameters, datasource);
// Fill the report, get the JasperPrint that can be exported to desired format.
JasperPrint jasperPrint = JasperFillManager.fillReport(path, parameters);
Without a datas ource, the attribute
whenNoDataType="AllSectionsNoDetail"on theJasperReportelement must be set, otherwise an empty (blank) report will be generated.