Jdbc-exceptions
提供:Dev Guides
JDBC-例外処理
例外処理を使用すると、プログラムで定義されたエラーなどの例外条件を制御された方法で処理できます。
例外条件が発生すると、例外がスローされます。 スローされるという用語は、現在のプログラムの実行が停止し、コントロールが最も近い該当するcatch句にリダイレクトされることを意味します。 適用可能なcatch句が存在しない場合、プログラムの実行は終了します。
JDBC例外処理はJava例外処理に非常に似ていますが、JDBCの場合、最も一般的な例外は* java.sql.SQLException。*です。
SQLExceptionメソッド
SQLExceptionは、ドライバーとデータベースの両方で発生する可能性があります。 このような例外が発生すると、SQLException型のオブジェクトがcatch句に渡されます。
渡されたSQLExceptionオブジェクトには、例外に関する追加情報を取得するために使用できる次のメソッドがあります-
Method | Description |
---|---|
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. |
Exceptionオブジェクトから入手可能な情報を利用することで、例外をキャッチしてプログラムを適切に続行できます。 ここにtryブロックの一般的な形式があります-
例
さて、次のように上記の例をコンパイルしましょう-
間違ったデータベース名または間違ったユーザー名またはパスワードを渡して上記の例を試して、結果を確認してください。