wood burning stoves 2.0*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes NX: DuplicateKeyException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "NX: DuplicateKeyException" Watch "NX: DuplicateKeyException" New topic
Author

NX: DuplicateKeyException

Peter Yunguang Qiu
Ranch Hand

Joined: Nov 22, 2003
Posts: 99
My db Interface:
public long createRecord(String[] data)
throws DuplicateKeyException;
When or in what situation to throw duplicateKeyException?
Could someone give me help?
Thanks,
Peter


Peter
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17257
    
    6

If you go to create a record and the key is already in the database, then this is a duplicate key. In real databases you have primary key which must be unique. So if you try to create a new record where the primary key already exists the database will throw back an error.
This is what Sun is looking for here.
If you have a primary key that is a number and the current values in the database are
1
3
4
5
and you try to insert a new record with a primary key of 2, then it will let you, if you try to insert a new record with a primary key of 4, then it will throw an error.
Hope that helps.
Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Peter Yunguang Qiu
Ranch Hand

Joined: Nov 22, 2003
Posts: 99
Originally posted by Mark Spritzler:
If you go to create a record and the key is already in the database, then this is a duplicate key. In real databases you have primary key which must be unique. So if you try to create a new record where the primary key already exists the database will throw back an error.
This is what Sun is looking for here.
If you have a primary key that is a number and the current values in the database are
1
3
4
5
and you try to insert a new record with a primary key of 2, then it will let you, if you try to insert a new record with a primary key of 4, then it will throw an error.
Hope that helps.
Mark

---------------
Thanks Mark.
In the assignment, what field is the key? Because we cannot change the format of the datebase file, the only unique field is recNo and it is impossible to be duplicated.
Could you explain to me?
Thanks.
Peter
Bigwood Liu
Ranch Hand

Joined: Feb 26, 2003
Posts: 240
Hi,
That depends on your own decision. Someone said the combine of name and location is key.
Best,
Terry Martinson
Ranch Hand

Joined: Oct 18, 2003
Posts: 293
Peter -
From everything I've read out here on the forum, I understood that most folks are using the record number as the key. It's kind of an implied thing that you calculate based on the data file specs - i.e. the actual record number is NOT stored anywhere on the file.
About your point of being impossible to get the duplicate....
The scenario I have been thinking about is this:
In my createRecord(String [] data) method, I determine whether or not I can add a record into a previously deleted record's spot, or if I have to add a new record at the end of the file. Say I determine which of these I will do - what if some other client/thread grabs the spot that I am planning to use for my add. This might be a potential case where when I actually get around to doing my add, I would find a Duplicate out there already.
I have not dealt with this problem yet though. I plan to think about it more when I get to the locking portion of the assignment.
TJ


SCJP, SCJD, SCWCD, SCBCD
Peter Yunguang Qiu
Ranch Hand

Joined: Nov 22, 2003
Posts: 99
Thanks all of you. I am still not very clear.
Philippe Maquet
Bartender

Joined: Jun 02, 2003
Posts: 1872
Hi Peter,
You may find these few links useful :
http://www.coderanch.com/t/184254/java-developer-SCJD/certification/DuplicateKeyException
http://www.coderanch.com/t/183894/java-developer-SCJD/certification/NX-Duplicate-Key-when-updating
http://www.coderanch.com/t/183817/java-developer-SCJD/certification/Nx-Documenting-DuplicateKeyException
Best,
Phil.
Don Resnik
Greenhorn

Joined: Dec 06, 2003
Posts: 18
I have had a similar question about the DuplicateKeyException. I do not believe there is a unique key in the provided database. There could logically be multiple records with the exact same data (a hotel could have multiple rooms with the same specifications avaiable at the same time for the same price). If there was a room number field, then a unique combination could be established. I am planning on not throwing the DuplicateKeyException and explaining it in my designchoices.txt. My app adds the new row if there are no existing matching rows or there are matching rows that are vaild. If there is a matching row that is deleted, that row is validated and a new row is not added.
The other thing that bothers me about createRecord (and deleteRecord) is that they are not requirements for my GUI, so I am not including those funcitons in the GUI, even though I am writing the code and testing it.
I would appreciate any comments on my choices.
Thanks
Philippe Maquet
Bartender

Joined: Jun 02, 2003
Posts: 1872
Hi Don,
Welcome to this forum !
I do not believe there is a unique key in the provided database. There could logically be multiple records with the exact same data (a hotel could have multiple rooms with the same specifications avaiable at the same time for the same price). If there was a room number field, then a unique combination could be established.

Exactly ! That's why I wrote somewhere that if you implement a primary key in URLyBird (and hence the DuplicateKeyException), it *must be* optional.
I am planning on not throwing the DuplicateKeyException and explaining it in my designchoices.txt.

Looks like reasonable.
My app adds the new row if there are no existing matching rows or there are matching rows that are vaild. If there is a matching row that is deleted, that row is validated and a new row is not added.

Oops ! Do you mean that you may transform a "create" in an "update" ? If you do, I think you are breaking the create requirements.
Best,
Phil.
Don Resnik
Greenhorn

Joined: Dec 06, 2003
Posts: 18
Actually, my create requirements say that the method creates a new record or possibly reuses a deleted entry. I don't know if they used that wording to make it tricky, but I interpet reusing a deleted entry as changing the valid flag back to valid. While that does not "create" a new record, it does reuse the deleted entry.
Also, thanks for commenting so quickly. I have been working on this for a few months and I am almost ready to submit (at least I thought I was until I started reading some of these posts). My last big question is about RMI-client death (I just posted the question).
Thanks
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: NX: DuplicateKeyException