aspose file tools*
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

Michael Dreese
Ranch Hand

Joined: Apr 02, 2003
Posts: 45
Hi all,
In my assignment (about contractors), I have a provided "create" method which throws an exception caled DuplicateKeyException.
I don't understand in what situation I have to throw DuplicateKeyException.
The following is the provided comment for "create" method from Sun:
"Creates a new record in the database (possibly reusing a deleted entry). Inserts the given data, and returns the record number of the new record."
I'm confused because it has nothing to mention about any key.
I'd appreciate any helps.
Cheers,
Michael
[ April 07, 2003: Message edited by: Michael Dreese ]
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17257
    
    6

This is basically used if the create method is called with a new Contractor whose ID or Record number is the same with one that already exists in the db.db file.
It is like trying to add a new record to a database with the primary key being the same as another record's primary key. Since primary key is to be unique, trying to add one with the same key is an error.
Hope that helps clear things up
Mark


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

Joined: Feb 13, 2002
Posts: 73
Hi,
I think that you don't have to implement the create / delete methods from the interface.
Greetings,
TK
Michael Dreese
Ranch Hand

Joined: Apr 02, 2003
Posts: 45
Originally posted by Mark Spritzler:
This is basically used if the create method is called with a new Contractor whose ID or Record number is the same with one that already exists in the db.db file.
It is like trying to add a new record to a database with the primary key being the same as another record's primary key. Since primary key is to be unique, trying to add one with the same key is an error.
Hope that helps clear things up
Mark

Mark, I think you are correct and thanks for your reply.
However, my db file doesn't have any contractor ID. It only has name, location, specialties, size, rate, and owner. I think the way to find out the duplicated key is by looking at two fileds - name and location (candiate keys?). Am I on the right track?
Cheers,
Michael
Michael Dreese
Ranch Hand

Joined: Apr 02, 2003
Posts: 45
Originally posted by Thomas Kijftenbelt:
Hi,
I think that you don't have to implement the create / delete methods from the interface.
Greetings,
TK

Thanks for your reply. I thought about that too because it seems the clients (home owner) will not use create/delete methods. However, for the further enhancement of the application, each contractor should be able to edit/create/delete their profiles in the database. I just want to be save to implement them.
Cheers,
Michael
Ramesh kumaar
Ranch Hand

Joined: Mar 19, 2002
Posts: 146
Hi,
You better have a method "private boolean duplicateCheck(String []data)", here u enumarate on the existing record values one by one and perform a check on(name location owner fields) if the current data is same as any of the existing record return TRUE else FALSE
Based on the about flag throw the exception(DuplicateKeyException).
-rameshkumar
Thomas Kijftenbelt
Ranch Hand

Joined: Feb 13, 2002
Posts: 73
Hi,
I am working on the assignment (contractor), and I must admit I am also a bit confused about the recordNumber (which is a long); it simply is not present in the database file.
Further I have a question about the field "size" (number of staff in the organization). The description states: the number of workers available when this record is booked.
Should you create separate records in the database for each customer who books one or more workers; and if that is the case, what do you do when one of the records is for sale again (do you update all other records)?
Greetings,
TK
Thomas Kijftenbelt
Ranch Hand

Joined: Feb 13, 2002
Posts: 73
a small addition: the question about the unique key is answered in post http://www.coderanch.com/t/183198/java-developer-SCJD/certification/NX-No-Primary-key
the suggestion is to use the record number (1,2,3, etc.) as unique key.
this only leaves the question about database updates open.
Michael Dreese
Ranch Hand

Joined: Apr 02, 2003
Posts: 45
Originally posted by Thomas Kijftenbelt:
I am working on the assignment (contractor), and I must admit I am also a bit confused about the recordNumber (which is a long); it simply is not present in the database file.

My recordNumber is a int. (I figure that out from my create method from DB interface.)
The recordNumber is not present in the database file and we have to assume that first record is recordNumber 0, second record is recordNumber 1, .., etc.

Further I have a question about the field "size" (number of staff in the organization). The description states: the number of workers available when this record is booked.
Should you create separate records in the database for each customer who books one or more workers; and if that is the case, what do you do when one of the records is for sale again (do you update all other records)?

I think when the record is booked, the booking is for the whole record, not the number of workers in certian organization.
Cheers,
Michael
Martin May
Greenhorn

Joined: Dec 12, 2002
Posts: 5
My assignment is for booking hotels, but it seems similar enough. The fact that recNo is an int suggests that they mean the record index (or position) in the data file.
That makes sense, until you consider that when you are deleting a record, the recNo for all the records following that one would change (i.e. if I delete record 5, record 6 becomes record 5, record 7 becomes record 6, etc.).
Alright, so I'll make use of the deleted flag then. That way record numbers won't change. When I add a new record, I first scan through the DB to see if I can reuse any of the deleted entries.
There's just one requirement which contradicts this design: the delete method says in its comment that it should make the associated disk storage available for reuse. Now, does that mean for reuse by the program when it add a new record, or immediately (i.e. get rid of the data right away)?
Any insights?
Thanks,
Martin
shan chen
Ranch Hand

Joined: Apr 09, 2003
Posts: 70

Originally posted by Martin May
the delete method says in its comment that it should make the associated disk storage available for reuse. Now, does that mean for reuse by the program when it add a new record, or immediately (i.e. get rid of the data right away)?

Martin,
My assignment is for booking too.
I think reuse means with the delete flag, the program can deem the deleted record space as reusable for adding new record later. It is possible for fixed-length record.
I have a question for DuplicateKeyException: If we just take record number as key, DuplicateKeyException will never be thrown!
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17257
    
    6

Actually Record number is the Key. It is where you check to see if the record number has been already used, and if it has, then the DuplicateKeyException is thrown.
Mark
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: NX: DuplicateKeyException