0

我尝试创建动态交叉表。当我尝试时,我有一个错误java.lang.ClassCastException Jasper Report Crosstab,而不是我解决它。但是我的报告是空的。我不明白为什么。我检查我的结果集是否为空。但它不为空。

这是我的代码:

public class DynamicJasperTemplate{
.....//variables define here    

public void buildReport() throws Exception{
    ....//I create query here and get row column and measure field name
    JRDataSource ds = getDataSource(query,a,b,c);//I get data from db as jrdatasource
    DynamicReport dr = buildReportLayout(a,b,c,ds); // build report layout and add it crosstab in this method.

    params.put("sr",ds);//I set report parameter value

    JasperReport jr = DynamicJasperHelper.generateJasperReport(dr, new ClassicLayoutManager(), params);
    JasperPrint jp = JasperFillManager.fillReport(jr, params, ds);
    JasperExportManager.exportReportToPdfFile(jp,"C:/report-out.pdf");

}

private DynamicReport buildReportLayout(String[] a,String[] b,String[] c, JRDataSource ds) {
    FastReportBuilder drb = new FastReportBuilder();
    drb.setWhenNoDataAllSectionNoDetail();
    initStyles();
    CrosstabBuilder cb = new CrosstabBuilder();


    cb.setHeight(200)
                .setWidth(500)
                .setHeaderStyle(mainHeaderStyle)
                .setDatasource("sr",DJConstants.DATA_SOURCE_ORIGIN_REPORT_DATASOURCE, DJConstants.DATA_SOURCE_TYPE_JRDATASOURCE)
                .setUseFullWidth(true)
                .setColorScheme(4)
                .setAutomaticTitle(true)
                .setCellBorder(Border.PEN_1_POINT());

    Object obj="NUMBER";
    Object obj1="VARCHAR2";
    String type = null;
    for(int i=0; i<a.length; i++) {
        DJCrosstabRow row = new CrosstabRowBuilder().setProperty(a[i],String.class.getName())
                            .setHeaderWidth(100).setHeight(0)
                            .setTitle(a[i])
                            .setShowTotals(true).setTotalStyle(totalStyle)
                            .setTotalHeaderStyle(totalHeader).setHeaderStyle(colAndRowHeaderStyle)
                            .build();
        cb.addRow(row);
    }

    DJCrosstabColumn col =new CrosstabColumnBuilder().setProperty(b.toString(),"java.sql.Timestamp")
                                    .setHeaderHeight(60).setWidth(50)
                                    .setTitle(b.toString()).setShowTotals(true)
                                    .setTotalStyle(totalStyle).setTotalHeaderStyle(totalHeader)
                                    .setHeaderStyle(colAndRowHeaderStyle)
                                    .build();
    cb.addColumn(col);
    cb.addMeasure(c[0],"java.math.BigDecimal", DJCalculation.NOTHING , c[0],measureStyle);      
    djcross = cb.build();
    drb.addHeaderCrosstab(djcross);
    drb.setUseFullPageWidth(true);
    drb.addParameter("sr", "java.util.Collection");

    DynamicReport dr = drb.build();
    return dr;
}


private void initStyles() {
    ....//here ı define styles
}



private JRDataSource getDataSource(String query,String[] a,String[] b,String[] c) throws SQLException, JRException {

    Connection con = new getConnection().conn();
    List<Map<String, ?>> arr = new ArrayList<Map<String, ?>>();
    String columnValue;
    Map data = new HashMap();
    JRResultSetDataSource result = null;
    ResultSet rs = null;
    int i;
    try{
        if(con!=null){
                Statement stmt = con.createStatement();
                rs = stmt.executeQuery(query);  
                JRDataSource ds = new JRResultSetDataSource(rs);
                return ds;
        }
    ......

我希望你能告诉我我做错了什么。

4

1 回答 1

0

我在这里解决了我的问题解决方案:当我删除drb.addParameter("sr", "java.util.Collection");并且b.toString()我因为未知列更改它时出错 b[0] 比我的问题解决了。

于 2012-09-11T07:21:48.413 回答