sailaja
sailaja

Reputation: 429

How to insert only unique values from a CSV file in Oracle database?

I am uploading a CSV file using a Servlet and inserting it in an Oracle table using JDBC. I need to insert only the unique values in the CSV file. If the values in CSV file is already in the database table, then it should not be inserted in the table. So it should insert only unique values from the CSV file.

Upvotes: 1

Views: 1643

Answers (3)

Sunila Gollapudi
Sunila Gollapudi

Reputation: 41

These options should help avoid an additional DB call to handle this situation.

Option 1: Very Simple and needs least coding ... but works when there is no global transaction boundary.

Just get all the inserts going, in case of any Constraint exception, just catch it and do "nothing", loop to another value

Option 2: Every time you read row from CSV, add it to a collection, before adding, just check if the object already exists (ex: arrayList.contains (object instance)) and continue adding only when there is no object with similar data). At the end, do a bulk insert. Note: If the data is large, go for fixed set of data for bulk insert.

Upvotes: 4

Dany Y
Dany Y

Reputation: 7041

i'm guessing the way ur inserting data in the database, is in a loop, you read from the csv and insert in the DB, so why not simply do a select satetement to check if the value exists and if it does don't insert it

Upvotes: 0

KV Prajapati
KV Prajapati

Reputation: 94643

Consider these steps:

  1. Read a value from the CSV file
  2. Code to search a "value" against the database and if it not found then insert it.

Upvotes: 1

Related Questions