This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes DuplicateKeyException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "DuplicateKeyException" Watch "DuplicateKeyException" New topic
Author

DuplicateKeyException

Keith Jones
Ranch Hand

Joined: Oct 30, 2006
Posts: 105
Our create methods are supposed to throw this but there is no key parameter that the create method takes and furthermore the key is the record number and there is no way that we can have two of those since all we need to do is append a record to the end of the file and hey presto we have a new key number.

Anyone have any ideas about this?


SCJP 1.4
Mark Smyth
Ranch Hand

Joined: Feb 04, 2004
Posts: 288
Originally posted by Keith Jones:
Our create methods are supposed to throw this but there is no key parameter that the create method takes and furthermore the key is the record number and there is no way that we can have two of those since all we need to do is append a record to the end of the file and hey presto we have a new key number.

Anyone have any ideas about this?


Yeah there is no key so this exception can never be thrown as the datafile currently stands. What I hav done is to drop the exception from my implementation of the DBMain interface.



You should use a reference of type DBMain instead of type Data in the client code that uses Data. You then make provision to handle the DuplicateKeyException as normal, even though in the current implementation of DBMain it can never be thrown. This ensures that the client code will not need to change should a later implementation of DBMain have reason to throw a DuplicateKeyException such as by adding in a recordID field.


[ November 22, 2006: Message edited by: Mark Smyth ]

SCJP<br />SCJD
Jeroen T Wenting
Ranch Hand

Joined: Apr 21, 2006
Posts: 1847
Just because it's in the throws clause doesn't mean it must be thrown
The exception is declared for future use and other database implementations.

And you don't just need to append new records, you should (if you want to do a good job at least) reuse space in the file taken up by deleted records.


42
Keith Jones
Ranch Hand

Joined: Oct 30, 2006
Posts: 105
Of course I know that we can reuse deleted records but I thought it would be better to just append because then we have a history of deleted items for reporting purposes. Besides neither option is wrong.
Chulwoo Choi
Ranch Hand

Joined: Apr 11, 2005
Posts: 65
Of course I know that we can reuse deleted records but I thought it would be better to just append because then we have a history of deleted items for reporting purposes. Besides neither option is wrong.


I also chose to do so. And as Jeroen said (in another post):
Whatever you decide, as always, document it

to defend your choices.

Regards,
Chulwoo
Keith Jones
Ranch Hand

Joined: Oct 30, 2006
Posts: 105
Thanks everyone.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: DuplicateKeyException
 
Similar Threads
NX: DuplicateKeyException
Matching data between two collections
Dealing with Deleted Records
Workflow
JTable setAutoCreateRowSorter method and getting key from maps