| Author |
How to Prevent Multiple entries stored in database
|
Rajendra Prakash
Ranch Hand
Joined: Sep 10, 2009
Posts: 293
|
|
Consider am using java , struts, hibernate and oracle. How can i prevent duplicate entries stored in
database. One way is to make field as Unique . For example i am entering name "prakash" in jsp page ,
prakash is already available means how can i prevent it.
|
 |
Wouter Oet
Saloon Keeper
Joined: Oct 25, 2008
Posts: 2700
|
|
|
What is wrong with making it unique in the database?
|
"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
|
 |
Rajendra Prakash
Ranch Hand
Joined: Sep 10, 2009
Posts: 293
|
|
|
How to prevent duplicate entries programmatically , not in DB.
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19216
|
|
|
You'd need to check before each insert whether or not there is a conflicting entry in the database.
|
SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
|
 |
Bharath Raja
Ranch Hand
Joined: Jan 21, 2009
Posts: 111
|
|
Rajendra Prakash wrote:How to prevent duplicate entries programmatically , not in DB.
read a field one by one and compare it with our new entry using loops... But wait a minute... this is very vague idea...let us wait for some ones better solution..
|
Life is either daring something or nothing - Helen Keller
|
 |
Rajendra Prakash
Ranch Hand
Joined: Sep 10, 2009
Posts: 293
|
|
|
Yes i want to check before each insert whether or not there is a conflicting entry in the database.
|
 |
Paul Sturrock
Bartender
Joined: Apr 14, 2004
Posts: 10336
|
|
|
Of course, that is not going to be a 100% guaranteed mechanism. How do you prevent another process inserting duplicate data after your check but before your insert without pessimistically locking the table? And why use pessimistic locking when databases provide a mechanism already in the form of unique indexes?
|
JavaRanch FAQ HowToAskQuestionsOnJavaRanch
|
 |
Rajendra Prakash
Ranch Hand
Joined: Sep 10, 2009
Posts: 293
|
|
|
its ok . I also want to know ,how can we do after inserting a record
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19216
|
|
|
You can't anymore, because the uniqueness is already violated.
|
 |
Wouter Oet
Saloon Keeper
Joined: Oct 25, 2008
Posts: 2700
|
|
To check is there is a double you need all the information so you have two options:
Hold all the information in memory (then what's the use of the database?)
Contact the database to check if the element already exists.
If you use one of these options you'll need to check for concurrent access and exposure of your datastructure.
The best solution is to set a constraint on the database.
|
 |
 |
|
|
subject: How to Prevent Multiple entries stored in database
|
|
|