Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7810473
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T03:47:48+00:00 2026-06-02T03:47:48+00:00

I have an excel sheet with about 25,000 rows. Each row in the excel

  • 0

I have an excel sheet with about 25,000 rows. Each row in the excel sheet will be a row in my table as well. I tried to do the following and it just keeps me giving Memory out of bound exception. I tried to change the batchSize from 25 to 50, 100, 500. None of them works. Can anyone tell me what am I doing wrong? changing the heap size of the JVM is not an option for me.

public void saveForecast(List list) throws FinderException{
    final Session session = getCurrentSession();
    final int batchSize = 25;
    Connection con = null;
    PreparedStatement pstmt = null;
    Iterator iterator = list.iterator();
    int rowCount = list.size();
    String sqlStatement = "INSERT INTO DMD_VOL_UPLOAD (ORIGIN, DESTINATION, DAY_OF_WEEK, EFFECTIVE_DATE, DISCONTINUE_DATE, VOLUME)";
    sqlStatement += " VALUES(?, ?, ?, ?, ?, ?)";
    System.out.println(sqlStatement);
    System.out.println("Number of rows to be inserted: "+ rowCount);
    System.out.println("Starting time: "+new Date().toString());
    try{
        con = session.connection();
        for(int i=0; i<rowCount; i++){
            ForecastBatch forecastBatch = (ForecastBatch) iterator.next();              
            pstmt = con.prepareStatement(sqlStatement);             
            pstmt.setString(1, forecastBatch.getOrigin());
            pstmt.setString(2, forecastBatch.getDestination());
            pstmt.setInt(3, forecastBatch.getDayOfWeek());

            java.util.Date effJavaDate = forecastBatch.getEffectiveDate();
            java.sql.Date effSqlDate = new java.sql.Date(effJavaDate.getTime());                
            pstmt.setDate(4,  effSqlDate);              
            java.util.Date disJavaDate=forecastBatch.getDiscontinueDate();
            java.sql.Date disSqlDate =  new java.sql.Date(disJavaDate.getTime());   

            pstmt.setDate(5, disSqlDate);               
            pstmt.setInt(6, forecastBatch.getVolumeSum());

            pstmt.addBatch();
            if(i % batchSize == 0){
                pstmt.executeBatch();
                session.flush();
                session.clear();
            }
        }
        pstmt.executeBatch();
        pstmt.close();
        System.out.println("Ending Time: "+ new Date().toString());
    }catch(SQLException e){
        e.printStackTrace();
        throw new FinderException(e);
    }
    finally{
        HibernateUtil.closeSession();
    }
}

}

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T03:47:50+00:00Added an answer on June 2, 2026 at 3:47 am

    You are creating a new statement inside your loop but only closing the last statement after the loop ends. That means you’re actually creating 25000 statements and closing only a single one leaving 24999 statements open, which I’m not surprised is causing you to run out of resources.

    Furthermore, you’re not using the batch statements correctly (you’d have to create the statement once, then set the parameters, call addBatch, set more parameters, call addBatch again, and so on, then call executeBatch when you want to submit all values in the batch.

    EDIT:

    You’ll probably fix this by moving the prepareStatement call just before the for loop and I don’t think calling session flush/clear is necessary either.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an excel sheet which has about 150,000 records, operations like find replace,
I have an Excel report with a bunch of sheets, each with a table
So I've got this Excel sheet of employees which have all the information about
I have a list of rows returned from an excel sheet. I want to
I have an Excel spreadsheet with 1 column, 700 rows. I care about every
I have customer file in Excel (using 2003) with about 16000 rows. The columns
I have an excel sheet laid out like so: +--id---+--val--+--timestamp--+ | 01 | 1.2
I have an excel sheet that has several headers. One of the header is
I have created Excel Sheet using Java program. It works fine. My problem is,
I have a Excel sheet with two columns and I need to create new

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.