user1129947
user1129947

Reputation: 49

Table tool in showing one fewer/lesser database records

I'm using JasperReports. In that am using table and java bean datasource. But while I send the data from my java class to jrxml table it displays one less record. For example if we have four records in my java class but when exported it to report it is displaying only three records. Data Bean is as follows: public class DataBean implements Serializable {

private static final long serialVersionUID = 1L;

private String description;
private String location;
private Date date;
private String type;
private String state;
private String comments;

/**
 * Parameterized constructor.
 * 
 * @param description
 *            description of alarm.
 * @param location
 *            location of alarm.
 * @param date
 *            date of alarm.
 * @param type
 *            type of alarm.
 * @param state
 *            state of alarm.
 * @param note
 *            note/comments of alarm.
 */
public DataBean(String description, String location, Date date,
        String type, String state, String note) {
    this.description = description;
    this.location = location;
    this.date = date;
    this.type = type;
    this.state = state;
    this.comments = note;
}

/**
 * Getter method to get the
 * 
 * @return location
 */
public String getLocation() {
    return location;
}

/**
 * Setter method to set the location.
 * 
 * @param location
 */
public void setLocation(String location) {
    this.location = location;
}

/**
 * Getter method to get the date.
 * 
 * @return date
 */
public Date getDate() {
    return date;
}

/**
 * Setter method to set the date.
 * 
 * @param date
 */
public void setDate(Date date) {
    this.date = date;
}

/**
 * Getter method to get the type.
 * 
 * @return type
 */
public String getType() {
    return type;
}

/**
 * Setter method to set the type.
 * 
 * @param type
 */
public void setType(String type) {
    this.type = type;
}

/**
 * Getter method to get the state.
 * 
 * @return state
 */
public String getState() {
    return state;
}

/**
 * Setter method to set the state.
 * 
 * @param state
 */
public void setState(String state) {
    this.state = state;
}

/**
 * Getter method to get the description.
 * 
 * @return description
 */
public String getDescription() {
    return description;
}

/**
 * Setter method to set the description.
 * 
 * @param description
 */
public void setDescription(String description) {
    this.description = description;
}

/**
 * Getter method to get the comments.
 * 
 * @return comments
 */
public String getComments() {
    return comments;
}

/**
 * Setter method to set the comments.
 * 
 * @param comments
 */
public void setComments(String comments) {
    this.comments = comments;
}

}

My main class is as follows: List beanCollection = new ArrayList();

    for (int i = 0; i < 2; i++) {
        DataBean bean = new DataBean("Alarm Description", "Location",
                new Date(), "EventScheduleStopped-12", "AlarmAcknowledged",
                "This is the test note for the alarm.");
        beanCollection.add(bean);
    }
    System.out.println(beanCollection.size());
    List<FilterBean> filterCollection = new ArrayList<FilterBean>();
    FilterBean filterBean = new FilterBean("Last 7 Days", "Any Type",
            "Open");
    filterCollection.add(filterBean);

    InputStream inputStream = ReportManagementController.class
            .getResourceAsStream("/report.jrxml");

    JRBeanCollectionDataSource beanColDataSource = new JRBeanCollectionDataSource(
            beanCollection);

    JRBeanCollectionDataSource filterBeanCollectionDataSource = new JRBeanCollectionDataSource(
            filterCollection);

    Map parameters = new HashMap();
    parameters.put(SHOW_COMMENTS, true);
    parameters.put(SHOW_FILTERS, false);
    parameters.put("logo",
            ReportManagementController.class.getResource(NEW_IMAGE_NAME)
                    .getPath());
    parameters.put("TableDataSource", beanColDataSource);
    parameters.put("filterDataSource", filterBeanCollectionDataSource);
    JasperDesign jasperDesign = JRXmlLoader.load(inputStream);
    JasperReport jasperReport = JasperCompileManager
            .compileReport(jasperDesign);
    JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport,
            parameters, beanColDataSource);

    JasperViewer.viewReport(jasperPrint);

and my jrxml (only displaying part content of jrxml as it is too big) is as follows:

parameter name="TableDataSource" class="net.sf.jasperreports.engine.JRDataSource"/

dataSourceExpression>![CDATA[$P{TableDataSource}]]>dataSourceExpression> Somebody help me please.

Upvotes: 1

Views: 2455

Answers (1)

sanBez
sanBez

Reputation: 933

It is known feature (or bug?) of JR.

To solve this problem pass collection (not JRDataSource!) as parameter and use this collection in dataSource for table.

For example:

parameters.put("beanCollection", beanCollection);
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport,
            parameters, new JREmptyDataSource());

In jrxml define parameter "beanCollection" as java.util.Collection

and define DataSource expression for table in jrxml as

new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($P{beanCollection})

Upvotes: 4

Related Questions