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 8561329
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T16:28:44+00:00 2026-06-11T16:28:44+00:00

I am importing huge amount of data from CSV files into MSSQL Server 2008.

  • 0

I am importing huge amount of data from CSV files into MSSQL Server 2008. I am using core JDBC (Without any ORM frameworks) and communicating with the DB using the driver ‘sqljdbc4.jar’ provided by Microsoft.

Following is my requirements:

  1. Parse the CSV file sequentially.
  2. Validate the record based on business requirements.(Log the error against each record in error file, if the record is invalid.)
  3. Import the records which is not found invalid into DB. (Log the error against each record in error file, if the record is invalid.)
  4. Save two resultant files success and error. Success will have the same number of fields with good records. Error file will have one additional field ‘ERROR’ with the error (Logged from validate / import face).

As of now am importing the records one by one. Which takes considerable amount of time even though am using PreparedStatement.

I am not using batch import since I need to log the exact error into the Error file.

Kindly suggest any an idea to improve the performance with out sacrificing the accurate error logging. And I am forced to do this without any ORM tools.

Here is the sample code:

for (Map<String, String> csvRecord : csvAsList) {
    // Prepare category object using csvRecord.
    // invoke obj.insert(category);
}

public Category insert(Category category){

if (category == null) {
   return null;
}

String SQL = "INSERT INTO t1(c1,c2) VALUES(?,?)";
PreparedStatement pstmt = null;
ResultSet rs = null;

try {

   pstmt = this.dbConnectionUtil.getConnection().prepareStatement(SQL,
           Statement.RETURN_GENERATED_KEYS);

   pstmt.setInt(1, category.getField1());
   pstmt.setString(2, category.getField2());

   int result = pstmt.executeUpdate();

   if (result < 1) {
      return null;
   }

   rs = pstmt.getGeneratedKeys();
   if (rs.next()) {
      category.setId(rs.getInt(1));
   }

} finally {
   if (rs != null)
      rs.close();
   if (pstmt != null)
      pstmt.close();
   this.dbConnectionUtil.closeConnection();
}

return category;

}

Update on Sep 20, 2012.

I have modified the code so that to import one CSV file there will be only one PreparedStatement object will be created. The new code is below:

public void importCSV(){
   // Create a db connection if its null or closed.
   // Create PreparedStatement objects for selects and inserts if null or closed.

   for (Map<String, String> csvRecord : csvAsList) {
      // Prepare category object using csvRecord.
      // Check whether category exists in by.
      // Import files can have up to 1,00,000 records so tracking errors is critical.
      try{
         categoryDAO.findByName(categoryName,<PreparedStatement object>);
      }
      catch(Exception exp){
         // log this to error.csv file
      }
      // If its a new category import it to the db.
      try{   
         categoryDAO.insert(category,<PreparedStatement object>);
      }
      catch(Exception exp){
         // log this to error.csv file
      }
   }

   // Close PreparedStatement objects
   // Close DB Connection

}

public Category insert(Category category,PreparedStatement pstmt ) throws SQLException{

if (category == null) {
   return null;
}

ResultSet rs = null;
try {

   pstmt.setInt(1, category.getField1());
   pstmt.setString(2, category.getField2());

   int result = pstmt.executeUpdate();

   if (result < 1) {
      return null;
   }

   rs = pstmt.getGeneratedKeys();
   if (rs.next()) {
      category.setId(rs.getInt(1));
   }

} finally {
   if (rs != null)
      rs.close();
}
return category;

}

Thanks.

  • 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-11T16:28:45+00:00Added an answer on June 11, 2026 at 4:28 pm

    It looks like you are calling prepareStatement over again for each row of your input data. This will pretty much eliminate the performance gain from using PreparedStatement. Instead, create the PreparedStatement outside your loop, and inside your loop keep only the setInt, setString and executeUpdate calls.

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

Sidebar

Related Questions

I'm importing a huge amount of data using BCP, and I was curious about
I have read huge amount of data , Using Tcp connection and i want
After importing content from a legacy website using the Feeds module, I'm left with
Upon importing the JSONKit.h and JSONKit.m files into my project , i get the
I'm importing a function from an unmanaged DLL into C#. The C++ function signature
I am importing data from MySQL to PowerPivot. I have all of the relevant
I'm importing a .CSV into an application with the following: if (($handle = fopen($file,
I am importing records from XML files, and it is chewing up memory. I've
I need to serialize a huge amount of data (around 2gigs) of small objects
I need to import a huge csv data file (6880 columns) and I need

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.