There are several ways to create data source for a Dynamic Report.
//The fieldNames here must be the same as in the report columns
DRDataSource dataSource = new DRDataSource("fieldName1', "fieldName2", ....);
//Add a row of values corresponding to the fields listed above
dataSource.add(value1, value2, value3......);
report.setDataSource(dataSource);
//Create the bean
public class ReportData {
//the data type and field name must be the same as the corresponding column in the report
private String fieldName1;
private Integer fieldName2;
............
//getters and setters for all the fields
public String getFieldName1() {
return fieldName1
}
public void setFieldName1(String f1) {
fieldName1 = f1;
}
.............
}
//Create a Collection of the Bean with your data
List<ReportData> sourceData = new ArrayList<ReportData>();
ReportData data = new ReportData();
for (i=0; i<5; i++) {
data.setFieldName1("field one value");
data.setFieldName2(49);
............
sourceData.add(data);
data = new ReportData();
}
//Create the data source
JRDataSource dataSource = new JRBeanCollectionDataSource(sourceData);
report.setDataSource(dataSource);
//Create a Collection of the Map
List<Map<String, Object>> sourceData = new ArrayList<Map<String, Objec>>();
Map<String, Object> data = new HashMap<String, Object>();
for (int i=0; i<5; i++) {
data.put("fieldName1", "field one value");
data.put("filedName2", 49);
...........
sourceData.add(data);
data = new HashMap<String, Object>();
}
//Create the data source
JRDataSource dataSource = new JRMapCollectionDataSource(sourceData);
report.setDataSource(dataSource);
--------------------------------------------------------------------------------------------------------
If you have ever asked yourself these questions, this is the book for you. What is the meaning of life? Why do people suffer? What is in control of my life? Why is life the way it is? How can I stop suffering and be happy? How can I have a successful life? How can I have a life I like to have? How can I be the person I like to be? How can I be wiser and smarter? How can I have good and harmonious relations with others? Why do people meditate to achieve enlightenment? What is the true meaning of spiritual practice? Why all beings are one? Read the book free here.
1. DRDataSource
This is the most straightforward one. You create a DRDataSource with a list of the field names of the columns in the report. You then add data row by row to the data source.//The fieldNames here must be the same as in the report columns
DRDataSource dataSource = new DRDataSource("fieldName1', "fieldName2", ....);
//Add a row of values corresponding to the fields listed above
dataSource.add(value1, value2, value3......);
report.setDataSource(dataSource);
2. JRBeanCollectionDataSource
You need a Bean class to hold each row of the data in your report. This class must be public even though it is most likely an inner class. Otherwise a method not available error may occur.//Create the bean
public class ReportData {
//the data type and field name must be the same as the corresponding column in the report
private String fieldName1;
private Integer fieldName2;
............
//getters and setters for all the fields
public String getFieldName1() {
return fieldName1
}
public void setFieldName1(String f1) {
fieldName1 = f1;
}
.............
}
//Create a Collection of the Bean with your data
List<ReportData> sourceData = new ArrayList<ReportData>();
ReportData data = new ReportData();
for (i=0; i<5; i++) {
data.setFieldName1("field one value");
data.setFieldName2(49);
............
sourceData.add(data);
data = new ReportData();
}
//Create the data source
JRDataSource dataSource = new JRBeanCollectionDataSource(sourceData);
report.setDataSource(dataSource);
3. JRMapCollectionDataSource
You need a map to hold each row of the data in your report. The key is the fieldName of a column and value is your data.//Create a Collection of the Map
List<Map<String, Object>> sourceData = new ArrayList<Map<String, Objec>>();
Map<String, Object> data = new HashMap<String, Object>();
for (int i=0; i<5; i++) {
data.put("fieldName1", "field one value");
data.put("filedName2", 49);
...........
sourceData.add(data);
data = new HashMap<String, Object>();
}
//Create the data source
JRDataSource dataSource = new JRMapCollectionDataSource(sourceData);
report.setDataSource(dataSource);
--------------------------------------------------------------------------------------------------------
If you have ever asked yourself these questions, this is the book for you. What is the meaning of life? Why do people suffer? What is in control of my life? Why is life the way it is? How can I stop suffering and be happy? How can I have a successful life? How can I have a life I like to have? How can I be the person I like to be? How can I be wiser and smarter? How can I have good and harmonious relations with others? Why do people meditate to achieve enlightenment? What is the true meaning of spiritual practice? Why all beings are one? Read the book free here.
No comments:
Post a Comment