Results 1 to 11 of 11
  1. #1
    kasipandian is offline Member
    Join Date
    Feb 2008
    Posts
    7
    Rep Power
    0

    Default How to get error codes using java program

    Dear Friends,

    My requirement is to get the SQL error code through my program whenever the exception thrown. I've to show this error code in JSP. Now I'm getting a error codes with messages. I'm using spring frame work. So If there any database related exceptions I'm getting DataAccessException . I don't know how to get the error code from DataAccessException to . In SQLException we have a method getErrorCode(). However I'm not getting any SQLException, So I cannot use this. So please help me to get the error code from DataAccessException . Any help would be appreciated.

    Thanks
    Kasi.
    Last edited by kasipandian; 05-12-2008 at 03:27 PM.

  2. #2
    derrickD is offline Member
    Join Date
    Apr 2007
    Location
    USA
    Posts
    50
    Rep Power
    0

    Default

    Doesn't DataAccessException have a getMessage() method??
    I use that one and I am pretty sure I have seen the ORA errors on my screen when they occur.

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

  4. #4
    danielstoner's Avatar
    danielstoner is offline Senior Member
    Join Date
    Apr 2008
    Location
    Canada
    Posts
    191
    Rep Power
    7

    Default

    Try DataAccessException.getCause(). It should return a cause exception. You can walk the cause chain and maybe you can find the SQLException somewhere in there. The signature is:
    Java Code:
    public Throwable getCause()
    You can check the type of exception with instanceof.
    Daniel @ [www.littletutorials.com]
    Language is froth on the surface of thought

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Yep, but daniel I think it is possible only with JDBC. With Hibernate we can't do it. Correct me if I'm wrong.

  6. #6
    danielstoner's Avatar
    danielstoner is offline Senior Member
    Join Date
    Apr 2008
    Location
    Canada
    Posts
    191
    Rep Power
    7

    Default

    Honestly I didn't use Hibernate for a while now so I don't remember this detail.
    Daniel @ [www.littletutorials.com]
    Language is froth on the surface of thought

  7. #7
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

  8. #8
    kasipandian is offline Member
    Join Date
    Feb 2008
    Posts
    7
    Rep Power
    0

    Default

    Hi friends
    I'm using Spring DAO and getting connection from the server using JNDI look up. I can able to get the error message. However I want to display the error code in the JSP. Is there any way to do it?

    Thanks
    Kasi.

  9. #9
    derrickD is offline Member
    Join Date
    Apr 2007
    Location
    USA
    Posts
    50
    Rep Power
    0

    Default

    I am using struts2 and just catch the error in my action and then display it with an actionError("Error "+e.getMessage());
    It is displayed in the jsp then.

  10. #10
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Well explained error message is mush better. Why you guys worried about error codes. ;) There are much better ways to handle exceptions in Java.

  11. #11
    rjuyal's Avatar
    rjuyal is offline Senior Member
    Join Date
    Mar 2008
    Location
    Delhi, India
    Posts
    194
    Rep Power
    7

    Default

    'm using Spring DAO
    If you mean Spring Framework, then

    In your DaoImpl, throw the SQLException, pass it to controller via Model ( i guess you have done this already ). In controller catch the SQLexception. use
    Java Code:
    errors.reject ( "DB Error", new Object[] { new Integer ( sqlException.getErrorCode() ) , sqlException.getCause().getMessage()}, "DB Error: {0} : {1}"  );
    you can set this in your messages.properties also.
    i am the future

Similar Threads

  1. CRC check program 1 error
    By javakid9000 in forum New To Java
    Replies: 1
    Last Post: 03-19-2008, 05:04 AM
  2. Posting codes and help
    By Java_Man in forum New To Java
    Replies: 2
    Last Post: 02-16-2008, 03:15 AM
  3. Getting ASCII codes of character
    By gapper in forum New To Java
    Replies: 1
    Last Post: 02-02-2008, 09:42 AM
  4. Replies: 1
    Last Post: 08-07-2007, 07:34 AM
  5. Replies: 2
    Last Post: 07-30-2007, 08:55 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •