Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

inserting repeated values in primary key field

 
Heba Mahmoud
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34195
340
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Take a look at the merge query. It can handle doing something different when the key exists.
 
Heba Mahmoud
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2586
11
C++ Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Heba Mahmoud
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 445
Android Eclipse IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
^^

Just iterate them to find if any duplicate elements and remove it. insert the rest of the values to DB.
 
Jan Cumps
Bartender
Posts: 2586
11
C++ Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could query the table to check if the primary key already exists. If yes: don't insert, if no: insert.
Regards, Jan
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic