2011-02-16 62 views
0

如果我有數十萬行到達結果集,那麼它會給出內存不足錯誤。JXL中的java.lang.OutOfMemoryError

我已生成使用下面的代碼::

我使用IBM WebSphere服務器V6.0 XLS報告。

public void generateExcel(Map parms) { 

     Connection dbConnection = null; 
     CallableStatement dbStatement = null;  
     PreparedStatement dbPreparedStatement = null; 
     ResultSet rs = null; 

     try { 
       dbConnection = this.dbConnect(); 
       dbStatement = dbConnection.prepareCall("{ call generateReport()}"); 
       System.out.println("call riskControlSummaryReport("+startDate+","+endDate+")"); 
       rs = dbStatement.executeQuery(); 

       CachedRowSet crs = new CachedRowSet(); 
       crs.populate(rs); 

       ws.setLocale(new Locale("en", "EN")); 
       File xlsFile = new File("D:/report.xls"); 
       FileOutputStream fos = new FileOutputStream(xlsFile); 
       ws.setGCDisabled(true); 
       workbook = Workbook.createWorkbook(fos, ws); 
       WritableSheet s1 = workbook.createSheet("riskControlSummary"+employeeId, 0); 
       //Here write report in Excel File 
       writeDetailReport(s1, crs); 
       s1.setPageSetup(PageOrientation.LANDSCAPE, PaperSize.LETTER, 0.5, 0.25); 
       SheetSettings sh = s1.getSettings(); 
       sh.setScaleFactor(69); 

       workbook.write(); 
       workbook.close(); 
       fos.close(); 
     } catch (WriteException we) { 
      we.printStackTrace(); 
     }catch (Exception e) { 
      e.printStackTrace(); 
     } 

    } 
public int writeDetailReport(WritableSheet s1, CachedRowSet rs) throws WriteException, SQLException { 

     Label line = null; 
     Label line = null; 
     int row = 0; 

     String labelNames[] = {"Function","Category","RiskTitle","Level", 
       "Controls"}; 

     String dbColumnNames[] = {"Function","Category","Title","Level", 
       "Controls"}; 

     //set cell width 
     setCellWidth(s1,0,4000); 
     setCellWidth(s1,1,5000); 
     setCellWidth(s1,2,4000); 
     setCellWidth(s1,3,3000); 
     setCellWidth(s1,4,6000); 

     int labelLength = labelNames.length; 
     int dbLength = dbColumnNames.length; 

     //label 
     row++; 

     for(int i=0;i<labelLength;i++){ 
      line = new Label(i, row, labelNames[i], getArial8ptBold()); 
      s1.addCell(line); 
     } 

     row++; 

     //data list 
     while (rs.next()) { 
       for(int j=0;j<dbLength;j++) 
       { 
          line = new Label(j, row, RiskUtility.replaceBlankIfNull(rs.getString(dbColumnNames[j])).trim(), cellFormat); 
          s1.addCell(line); 
       } 
       row++; 
      }//end while 
     } 
     return row; 
    } 

回答

0

通常的解決方法:添加-Xmx128m到你的java命令,以增加堆空間。標準Java應用程序最初設置爲64 MByte。

當您使用應用程序服務器時 - AS自身以-Xmx參數啓動。看看它的價值,並增加它來爲整個服務器分配更多的堆空間。 (這將是遠遠超過128兆字節)

0
1. Why cant you get Records of 10k in a batch and write to the file. 

2. java -Xms512m -Xmx512m 

結果集RS填充CachedRowSet的後不立即關閉。

CachedRowSet crs = new CachedRowSet(CachedRowSet.TYPE_FORWARD_ONLY); 
crs.populate(rs); 
rs.close(); 
+0

你的概念是正確的,但我在JXL.because代替緩衝這裏IM使用WritableSheet.i沒有發現刷新緩衝區以薄板與創造新的instance..etc .. – Sweety 2011-02-16 12:48:28