Results 1 to 6 of 6
Like Tree4Likes
  • 1 Post By Norm
  • 2 Post By jashburn
  • 1 Post By gimbal2

Thread: JDBC: SQLException, What is this code doing?

  1. #1
    Zarah is offline Senior Member
    Join Date
    Mar 2014
    Posts
    116
    Rep Power
    0

    Default JDBC: SQLException, What is this code doing?

    Hi,

    Can somebody throw some light on what is (SQLException)e in

    Java Code:
    if (ignoreSQLException(
                    ((SQLException)e).
                    getSQLState()) == false) {
    and what is this entire piece of code doing?

    It is a part of the following method. It is given in a tutorial on JDBC.

    Java Code:
    public static void printSQLException(SQLException ex) {
    
        for (Throwable e : ex) {
            if (e instanceof SQLException) {
                if (ignoreSQLException(
                    ((SQLException)e).
                    getSQLState()) == false) {
    
                    e.printStackTrace(System.err);
                    System.err.println("SQLState: " +
                        ((SQLException)e).getSQLState());
    
                    System.err.println("Error Code: " +
                        ((SQLException)e).getErrorCode());
    
                    System.err.println("Message: " + e.getMessage());
    
                    Throwable t = ex.getCause();
                    while(t != null) {
                        System.out.println("Cause: " + t);
                        t = t.getCause();
                    }
                }
            }
        }
    }
    Thank you in advance.

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default Re: JDBC: SQLException, What is this code doing?

    what is (SQLException)e
    That casts the variable e to the type: SQLException
    So that the getSQLState() method can be called.
    Zarah likes this.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    jashburn is offline Senior Member
    Join Date
    Feb 2014
    Posts
    219
    Rep Power
    1

    Default Re: JDBC: SQLException, What is this code doing?

    Java Code:
    if (ignoreSQLException(((SQLException)e).getSQLState()) == false)
    To understand the code, work inside-out:
    • ((SQLException)e).getSQLState(): After casting e (which is of type Throwable as defined in the earlier for loop), it obtains the SQLException's SQL state code.
    • SQL state code: As stated in the tutorial, SQL "codes and their respective meanings have been standardized by ISO/ANSI and Open Group (X/Open)," with some codes reserved for vendor-specific definitions. Take a look at PostgreSQL: Documentation: 9.3: PostgreSQL Error Codes for an example.
    • (ignoreSQLException(((SQLException)e).getSQLState( )): After obtaining the SQL state code, the code is passed to the ignoreSQLException() method. You'd see further down in the tutorial that the latter method checks for certain SQL state codes, and returns true for a couple of codes that are deemed to be ignorable, and otherwise returns false so that the SQLException is processed by the application's regular exception handling routines.

    SQLExceptions are thrown by a number of methods in JDBC. E.g., take a look at the API docs for Connection (Java Platform SE 7 ), Statement (Java Platform SE 7 ) and ResultSet (Java Platform SE 7 ) for the reasons methods might throw this exception. See also SQLException (Java Platform SE 7 ) for the API documentation for this exception.
    Last edited by jashburn; 04-24-2014 at 03:17 PM. Reason: Applied gimbal2's suggestion
    gimbal2 and Zarah like this.

  4. #4
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,848
    Rep Power
    5

    Default Re: JDBC: SQLException, What is this code doing?

    Quote Originally Posted by jashburn View Post
    and otherwise returns false so that the SQLException is processed further.
    That is better written as "is processed by the application's regular exception handling routines" IMO, keeping with the in-depth spirit of the post which is to really leave nothing up for misinterpretation.
    jashburn likes this.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  5. #5
    jashburn is offline Senior Member
    Join Date
    Feb 2014
    Posts
    219
    Rep Power
    1

    Default Re: JDBC: SQLException, What is this code doing?

    Post edited per suggestion. Cheers, gimbal2.

  6. #6
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,848
    Rep Power
    5

    Default Re: JDBC: SQLException, What is this code doing?

    Yay I helped!
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. JDBC code in Interface
    By pranyrajesh in forum New To Java
    Replies: 4
    Last Post: 06-30-2012, 01:53 AM
  2. Replies: 5
    Last Post: 03-13-2012, 01:51 PM
  3. JDBC ODBC SQLException
    By JohnPringle83 in forum JDBC
    Replies: 6
    Last Post: 05-30-2011, 08:07 AM
  4. JDBC code not included into JAR file??
    By friday in forum Advanced Java
    Replies: 1
    Last Post: 12-17-2010, 07:13 PM
  5. Replies: 1
    Last Post: 10-16-2008, 02:09 PM

Posting Permissions

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