001/*
002 * Copyright 2002-2018 the original author or authors.
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 *      https://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
017package org.springframework.jdbc;
018
019import java.sql.SQLException;
020
021import org.springframework.dao.InvalidDataAccessResourceUsageException;
022import org.springframework.lang.Nullable;
023
024/**
025 * Exception thrown when a ResultSet has been accessed in an invalid fashion.
026 * Such exceptions always have a {@code java.sql.SQLException} root cause.
027 *
028 * <p>This typically happens when an invalid ResultSet column index or name
029 * has been specified. Also thrown by disconnected SqlRowSets.
030 *
031 * @author Juergen Hoeller
032 * @since 1.2
033 * @see BadSqlGrammarException
034 * @see org.springframework.jdbc.support.rowset.SqlRowSet
035 */
036@SuppressWarnings("serial")
037public class InvalidResultSetAccessException extends InvalidDataAccessResourceUsageException {
038
039        @Nullable
040        private final String sql;
041
042
043        /**
044         * Constructor for InvalidResultSetAccessException.
045         * @param task name of current task
046         * @param sql the offending SQL statement
047         * @param ex the root cause
048         */
049        public InvalidResultSetAccessException(String task, String sql, SQLException ex) {
050                super(task + "; invalid ResultSet access for SQL [" + sql + "]", ex);
051                this.sql = sql;
052        }
053
054        /**
055         * Constructor for InvalidResultSetAccessException.
056         * @param ex the root cause
057         */
058        public InvalidResultSetAccessException(SQLException ex) {
059                super(ex.getMessage(), ex);
060                this.sql = null;
061        }
062
063
064        /**
065         * Return the wrapped SQLException.
066         */
067        public SQLException getSQLException() {
068                return (SQLException) getCause();
069        }
070
071        /**
072         * Return the SQL that caused the problem.
073         * @return the offending SQL, if known
074         */
075        @Nullable
076        public String getSql() {
077                return this.sql;
078        }
079
080}