2012-09-10 55 views
0

我嘗試創建動態交叉表。當我嘗試時,我有一個錯誤java.lang.ClassCastException Jasper Report Crosstab比我解決它。但我的報告是空的。我不明白爲什麼。我檢查我的結果集是否爲空。但它不是空的。使用DynamicJasper API:使用交叉表獲取空報表

這裏是我的代碼:

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; 
     } 
    ...... 

我希望你能告訴我什麼,我作出錯誤的。

回答

0

我在這裏解決了我的問題解決方案: 當我刪除drb.addParameter("sr", "java.util.Collection");b.toString()我犯錯誤爲未知列更改它[b]比我的問題解決。