• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

NX: DuplicateKeyException

 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I think that you don't have to implement the create / delete methods from the interface.
Greetings,
TK
 
Michael Dreese
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
a small addition: the question about the unique key is answered in post https://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
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
reply
    Bookmark Topic Watch Topic
  • New Topic