简体   繁体   English

执行Java代码将数据库表数据导出到Excel工作表(.xlsx)时内存不足

[英]Out of memory while executing java code to export a DB table data to Excel sheet (.xlsx)

I am trying to export a table data from Database to Excel sheet. 我正在尝试将表数据从数据库导出到Excel工作表。 Since the data is huge I used XSSFWorkbook so that the data gets exported as xlsx file. 由于数据巨大,因此我使用XSSFWorkbook,以便将数据导出为xlsx文件。 I used setFetchsize() so that data is fetched from DB quicker. 我使用setFetchsize()以便更快地从数据库中获取数据。 I have a issue here, though it is fast it consumes my system memory and CPU usage. 我这里有一个问题,尽管速度很快,但它消耗了我的系统内存和CPU使用率。 I tried all the possible answers googling. 我尝试了所有可能的答案。 It will be of greater help if someone suggests me to proceed further. 如果有人建议我继续前进,将会有更大的帮助。

Implemented a Java class which establishes DB connection, once the connection is established the data from table is fetched to resultset, to improvise the fetch speed I used setFetchSize(1000) method. 实现了一个建立数据库连接的Java类,一旦建立了连接,就将表中的数据提取到结果集中,以提高提取速度,我使用了setFetchSize(1000)方法。 Running out of memory exactly after 80000 records. 完全在80000条记录后用尽了内存。 Tried with setFetchSize(10000) too. 也尝试过setFetchSize(10000)。

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Date;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class XSSF_Excel_Appender {
  private static Connection getConnection(){
    Connection con = null;
    String url = "connection string";
    try{
        Class.forName("driver class");
        con = DriverManager.getConnection(url,"username","password");
    }
    catch(ClassNotFoundException e){
        e.printStackTrace();
        System.out.println("Driver class not found.");
    }
    catch(SQLException e){
        e.printStackTrace();
        System.out.println("Exception occured while connecting DB");
    }
    return con;
  }

  public boolean getTableData(int range){
    boolean flag = true;
    ArrayList<Object[]> tableDataList = null;
    int num = 0;
    Connection con = getConnection();
    if(con != null){
        try{
            XSSFWorkbook workBook = new XSSFWorkbook();
            XSSFSheet sheet = workBook.createSheet("*******");
            XSSFRow headingRow = sheet.createRow(0);
            headingRow.createCell(0).setCellValue("*****");
            headingRow.createCell(1).setCellValue("*******");
            headingRow.createCell(2).setCellValue("*********");
            headingRow.createCell(3).setCellValue("*****");
            headingRow.createCell(4).setCellValue("******");
            headingRow.createCell(5).setCellValue("*****");
            Statement ps1=con.createStatement();
            ResultSet resultSet = ps1.executeQuery("SELECT COUNT(*) from 
            table_name");
            while(resultSet.next()) {
              num = Integer.parseInt(resultSet.getString(1));
            }
            System.out.println("Number of rows in (table_name) "+num);
            for(int i=0;i<100000;i+=range) {
                Statement ps2 = con.createStatement();
                ps2.setFetchSize(1000);
                ResultSet result = ps2.executeQuery("SELECT ROWNUM as 
                S_NO,(TABLE_NAME).* FROM Table_name offset "+i+" rows 
                fetch next "+range+" rows only");
                tableDataList = new ArrayList<Object[]>();
                while(result.next()) {
                    Object[] objArray = new Object[6];
                    objArray[0] = (result.getString(1) == null ? "Null" : 
                                   result.getString(1));
                    objArray[1] = (result.getString(2) == null ? "Null" : 
                                   result.getString(2));
                    objArray[2] = (result.getString(3) == null ? "Null" : 
                                   result.getString(3));
                    objArray[3] = (result.getString(4) == null ? "Null" : 
                                   result.getString(4));
                    objArray[4] = (result.getString(5) == null ? "Null" : 
                                   result.getString(5));
                    objArray[5] = (result.getString(6) == null ? "Null" : 
                                   result.getString(6));
                    tableDataList.add(objArray);
                }
                if(tableDataList != null && tableDataList.size() > 0){
                    int lastRow=sheet.getLastRowNum();
                    for (Object[] objects : tableDataList) {
                        XSSFRow row = sheet.createRow(++lastRow);
                        int colNum = 0;
                        for (Object field : objects) {
                            Cell cell = row.createCell(colNum++);
                            if (field instanceof String) {
                                cell.setCellValue((String) field);
                            } else if (field instanceof Integer) {
                                cell.setCellValue((Integer) field);
                            }
                        }
                    }
                }
            }
            String file = "Location to store the exported xlsx workbook";
            FileOutputStream fos = new FileOutputStream(new File(file));
            workBook.write(fos);
            fos.close();
          }catch(SQLException e){
             flag = false;
             e.printStackTrace();
             System.out.println("Unable to create PreparedStatement");
         }
        catch(FileNotFoundException e){
            e.printStackTrace();
            System.out.println("Invalid directory or file not found");
        }catch(IOException e){
            e.printStackTrace();
            System.out.println("Error while writing .xlsx to directory");
        }
    }
    return flag;
}

public static void main(String[] args) {
    Date d1 = new Date();
    try {
        System.out.println("Inside Main method time: "+d1);
        int range = 10000;
        XSSF_Excel_Appender exporter = new XSSF_Excel_Appender();
        if(exporter.getTableData(range)) {
            System.out.println("Successfully exported");
        }
        else{
            System.out.println("Some error has occurred");
        }
    }
    catch(Exception ex) {
        ex.printStackTrace();
    }
    Date d2 = new Date();
    System.out.println("End time : "+d2);
    long diff = d2.getTime() - d1.getTime();
    System.out.println("Total time taken (using Array List) : 
   "+diff/(60*1000)%60+" minutes,"+diff/1000%60+" seconds.");
  }
 }

The CPU usage is reaching 100% by the time of fetching 80000 records. 提取80000条记录时,CPU使用率已达到100%。 Expecting some speedy exporting method suggestions without impacting memory. 期待一些快速的导出方法建议,而不会影响内存。

You can set max heap size to run java. 您可以设置最大堆大小以运行Java。 with command-line option -Xmx . 使用命令行选项-Xmx

JAVA_ARGS="-Xmx1024m"

Look into this oracle documentation about this 查看有关此内容的oracle文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM