JDBC SQLException Handling with Programming Example

In this chapter you will learn

1. What is Exception Handling in JDBC?
2. How to fix SQLException in JDBC?
3. SQLException Programming Example

What is Exception Handling in JDBC?

Exception is such type of condition when program encounters problem in execution and quit with a problematic error message. In JDBC, when program gets trouble in data source, it throws SQLException. However, there are several many exceptions in Java but while we are studying JDBC, SQLException is most common and we will cover it through this chapter.

A SQLException can occur in JDBC Driver or inside database. In this chapter you will learn how to handle this exception.

SQLException Methods

MethodDescription
getErrorCode()Gets the error number associated with the exception.
getMessage()Gets the JDBC driver’s error message for an error, handled by the driver or gets the Oracle error number and message for a database error.
getSQLState()Gets the XOPEN SQLstate string. For a JDBC driver error, no useful information is returned from this method. For a database error, the five-digit XOPEN SQLstate code is returned. This method can return null.
getNextException()Gets the next Exception object in the exception chain.
printStackTrace()Prints the current exception, or throwable, and it’s backtrace to a standard error stream.
printStackTrace(PrintStream s)Prints this throwable and its backtrace to the print stream you specify.
printStackTrace(PrintWriter w)Prints this throwable and it’s backtrace to the print writer you specify.

Programming Example

Summary

In this chapter you learned what is SQLException in JDBC and how to handle it using exception handling.


Leave a Reply

Your email address will not be published. Required fields are marked *