JXL 中的 java.lang.OutOfMemoryError

发布于 2024-10-17 19:12:16 字数 2895 浏览 4 评论 0原文

如果结果集中有数十万行,则会出现内存不足错误。

我使用以下代码生成了 XLS 报告::

我正在使用 IBM websphere Server V6.0。

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

If I have hundreds of thousands of rows arrived in result set, then it gives out of memory error.

I have generated XLS report using following code::

I am using IBM websphere Server V6.0.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

带刺的爱情 2024-10-24 19:12:16

通常的解决方案:将 -Xmx128m 添加到 java 命令中以增加堆空间。对于标准 Java 应用程序,它最初设置为 64 MB。

当您使用应用程序服务器时,AS 本身是使用 -Xmx 参数启动的。查看它的值并增加它以为整个服务器分配更多的堆空间。 (将远远超过 128 MB)

The usual solution: add -Xmx128m to your java command to increase the heap space. It is set to 64 MByte initially for standard Java application.

As your using an application server - the AS itself is started with an -Xmx parameter. Have a look at it's value and increase it to assign more heap space to the entire server. (it will be much more than 128 MByte)

美男兮 2024-10-24 19:12:16
1. Why cant you get Records of 10k in a batch and write to the file.

2. java -Xms512m -Xmx512m 

填充 cachedRowset 后,ResultSet rs 不会立即关闭。

CachedRowSet crs = new CachedRowSet(CachedRowSet.TYPE_FORWARD_ONLY);
crs.populate(rs);
rs.close();
1. Why cant you get Records of 10k in a batch and write to the file.

2. java -Xms512m -Xmx512m 

The ResultSet rs is not closed immediately after the cachedRowset is populated.

CachedRowSet crs = new CachedRowSet(CachedRowSet.TYPE_FORWARD_ONLY);
crs.populate(rs);
rs.close();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文