aspose file tools*
The moose likes JDBC and the fly likes inserting repeated values in primary key field Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "inserting repeated values in primary key field" Watch "inserting repeated values in primary key field" New topic
Author

inserting repeated values in primary key field

Heba Mahmoud
Ranch Hand

Joined: Apr 26, 2009
Posts: 62
Hello

i want to send an array values to be inserted in table field that should be a primary key for the table

so when i try to insert a repeated value in that colume , sql excepption will be thrown

i want to insert all values that not repeated and escape any reapeated values without throwing an exception that make the insertion operation fails for all records even they not repeated ....

please i need help in this issue


SCJP Pass 85%....
Thousands of candles can be lighted from a single candle, and the life of the candle will not be shortened
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30749
    
156

Take a look at the merge query. It can handle doing something different when the key exists.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Heba Mahmoud
Ranch Hand

Joined: Apr 26, 2009
Posts: 62
really i want to solve problem from java side not from database side

so i want to prevent the sql exception from occuring and escaping the rest of the rows inserting when error in one row insert....

i solved it as check each string added as a primary key field in table to check that value isn't found before then insert it else don't do anything

so i did that as i don't know how to handle the exception when occur and make the program run successfully for the rest of rows that have no problem.....
Jan Cumps
Bartender

Joined: Dec 20, 2006
Posts: 2503
    
    8

Heba Mahmoud wrote: really i want to solve problem from java side not from database side....
You could first screen your array for duplicates, and remove them.


OCUP UML fundamental and ITIL foundation
youtube channel
Heba Mahmoud
Ranch Hand

Joined: Apr 26, 2009
Posts: 62
i read data from files then insert it in table

i read files and load them in set then iterate in set and insert all data of it in the table
the problem occurs when the files have repeated data "that already have been inserted "

and i don't load all the files at same time ..... so the PK faild as sql exception...
Rajkumar balakrishnan
Ranch Hand

Joined: May 29, 2008
Posts: 445

^^

Just iterate them to find if any duplicate elements and remove it. insert the rest of the values to DB.


Never try to be a hard-worker. Be a smart-worker.
My Blog
Jan Cumps
Bartender

Joined: Dec 20, 2006
Posts: 2503
    
    8

You could query the table to check if the primary key already exists. If yes: don't insert, if no: insert.
Regards, Jan
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: inserting repeated values in primary key field