2011-02-16 65 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