File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes How to handle database exceptions Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "How to handle database exceptions" Watch "How to handle database exceptions" New topic
Author

How to handle database exceptions

Hosh Nasi
Ranch Hand

Joined: Sep 10, 2004
Posts: 44
I have a gui program that displays a table and allows users to click on diffrent fields and change the data. Almost all these fields are unique to one record though. If the use input a duplicate unique value and tries to comit the changes the database throws an error that basically does nothing but say "Dupplicate key" What I want to do is put the cursor in the second duped field keeping the first one.

It sounds like I am not sure on the exception scheme because I am not. I made this in JBuilder, and I am not very proficiant in Java GUI developement.

Thank you!

**Looks like this is main source of exception catching.**

catch(Exception e) {
e.printStackTrace();
}
[ October 29, 2004: Message edited by: Hosh Nasi ]
Jeff Bosch
Ranch Hand

Joined: Jul 30, 2003
Posts: 804
In your catch statement, you could add another try-catch block and invoke the JTable's editCellAt method with the cell's row and column indexes. (The second try-catch is to catch the IllegalArgumentException if either of the two indexes is out of bounds of the available JTable dimensions.)


Give a man a fish, he'll eat for one day. <br />Teach a man to fish, he'll drink all your beer.<br /> <br />Cheers,<br /> <br />Jeff (SCJP 1.4, SCJD in progress, if you can call that progress...)
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
This has always been a difficult problem to address due to the nature of the typical application architecture:
  • Read data from database
  • User changes some data
  • Write all changes to database

  • Error handling is trivial if the user modifies (or adds or deletes) only one row, but this becomes difficult with more. Even when the client layer tracks all of the changes, writing code to determine which cell caused an error can be tiresome.

    One method I've used is to parse the exception message for the integrity constraint that was violated. You can use the Strategy pattern to supply an exception translator to your DAOs and implement one for each database dialect (MySQL, Oracle, etc) to which you deploy your application.

    Even that doesn't solve the entire problem. For example, you determine that the USER_EMAIL_UK constraint was violated, telling you the user has given two people the same email address. But the constraint name doesn't tell you which row caused the problem, so you still must search the table for duplicates.

    One easy out is to tell the user as much information as you can easily get and leave the rest to them: "You have specified a duplicate email address." Let the user sort it out.

    You can create a constraint name to error message (key) table for each window to allow for a generic framework.
    [ October 30, 2004: Message edited by: David Harkness ]
    Hosh Nasi
    Ranch Hand

    Joined: Sep 10, 2004
    Posts: 44
    Thanks guys for the replies. Upon further inspection I have learned that when the program does in fact throw the JDataStore dupplicate error it automatically selects the row with the dupe data. I can attribute this to only two things.

    1. JDataStore inputs data row by row from the resultset.
    2. There is some kind of internal handle that is using the JDataStore error to figure out what record is duped. If that is the case I just need to plug into that..

    Part of the TraceStack says

    David Harkness
    Ranch Hand

    Joined: Aug 07, 2003
    Posts: 1646
    Originally posted by Hosh Nasi:
    JDataStore inputs data row by row from the resultset.
    That would be my guess. Back when I wrote PowerBuilder GUI code, all data modifications (insert/update/delete) were handled row-by-row. Thus any SQL errors were raised for a single row, making placement of the cursor easy.
    Hosh Nasi
    Ranch Hand

    Joined: Sep 10, 2004
    Posts: 44
    I am very perplexed.. I am not sure where the error handleing is happeing within my program. I have put in many break points and added diffrent dialog boxes to test which exception path is being followed but nothing works I am confused..

    As you can see the blanket exception catch has a diaglo message before the printstack. However this is never displayed.



    [ November 02, 2004: Message edited by: Hosh Nasi ]

    [ November 02, 2004: Message edited by: Hosh Nasi ]
    [ November 02, 2004: Message edited by: Hosh Nasi ]
     
    I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
     
    subject: How to handle database exceptions
     
    Similar Threads
    Blank spaces in database fields
    Exception Chaining , Can it produce unnecessary coupling?
    read-test-insert, keep field unique
    DuplicateKeyException B&S
    Not getting new values in backing bean