001: /*
002: * Copyright 2004 Clinton Begin
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package com.ibatis.common.jdbc.exception;
017:
018: import java.sql.SQLException;
019:
020: /**
021: * Unchecked exception to allow passing an Exception with the original SQLException
022: */
023: public class RuntimeSQLException extends RuntimeException {
024:
025: /**
026: * Default constructor
027: */
028: public RuntimeSQLException() {
029: }
030:
031: /**
032: * Constructor to pass along a message
033: * @see com.ibatis.common.exception.RuntimeException
034: * @param msg - the message
035: */
036: public RuntimeSQLException(String msg) {
037: super (msg);
038: }
039:
040: /**
041: * Constructor to pass along another exception
042: * @see com.ibatis.common.exception.RuntimeException
043: * @param sqlException - the exception
044: */
045: public RuntimeSQLException(SQLException sqlException) {
046: super (sqlException);
047: }
048:
049: /**
050: * Constructor to pass along a message and an exception
051: * @see com.ibatis.common.exception.RuntimeException
052: * @param msg - the message
053: * @param sqlException - the exception
054: */
055: public RuntimeSQLException(String msg, SQLException sqlException) {
056: super (msg, sqlException);
057: }
058:
059: /**
060: * Getter for the SQL State
061: * @return - the state
062: */
063: public String getSQLState() {
064: Throwable cause = getCause();
065: if (cause instanceof SQLException) {
066: return ((SQLException) cause).getSQLState();
067: } else {
068: return null;
069: }
070:
071: }
072:
073: /**
074: * Getter for the error code
075: * @return - the error code
076: */
077: public int getErrorCode() {
078: Throwable cause = getCause();
079: if (cause instanceof SQLException) {
080: return ((SQLException) cause).getErrorCode();
081: } else {
082: return -1;
083: }
084: }
085:
086: /**
087: * Get the next exception in the chain
088: * @return - the next exception
089: */
090: public SQLException getNextException() {
091: Throwable cause = getCause();
092: if (cause instanceof SQLException) {
093: return ((SQLException) cause).getNextException();
094: } else {
095: return null;
096: }
097: }
098:
099: /**
100: * Set the next exception in the chain
101: * @param ex - the next exception
102: */
103: public synchronized void setNextException(SQLException ex) {
104: Throwable cause = getCause();
105: if (cause instanceof SQLException) {
106: ((SQLException) cause).setNextException(ex);
107: }
108: }
109:
110: }
|