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.core; 018 019import java.sql.ResultSet; 020import java.sql.SQLException; 021 022import org.springframework.dao.DataAccessException; 023import org.springframework.lang.Nullable; 024 025/** 026 * Callback interface used by {@link JdbcTemplate}'s query methods. 027 * Implementations of this interface perform the actual work of extracting 028 * results from a {@link java.sql.ResultSet}, but don't need to worry 029 * about exception handling. {@link java.sql.SQLException SQLExceptions} 030 * will be caught and handled by the calling JdbcTemplate. 031 * 032 * <p>This interface is mainly used within the JDBC framework itself. 033 * A {@link RowMapper} is usually a simpler choice for ResultSet processing, 034 * mapping one result object per row instead of one result object for 035 * the entire ResultSet. 036 * 037 * <p>Note: In contrast to a {@link RowCallbackHandler}, a ResultSetExtractor 038 * object is typically stateless and thus reusable, as long as it doesn't 039 * access stateful resources (such as output streams when streaming LOB 040 * contents) or keep result state within the object. 041 * 042 * @author Rod Johnson 043 * @author Juergen Hoeller 044 * @since April 24, 2003 045 * @param <T> the result type 046 * @see JdbcTemplate 047 * @see RowCallbackHandler 048 * @see RowMapper 049 * @see org.springframework.jdbc.core.support.AbstractLobStreamingResultSetExtractor 050 */ 051@FunctionalInterface 052public interface ResultSetExtractor<T> { 053 054 /** 055 * Implementations must implement this method to process the entire ResultSet. 056 * @param rs the ResultSet to extract data from. Implementations should 057 * not close this: it will be closed by the calling JdbcTemplate. 058 * @return an arbitrary result object, or {@code null} if none 059 * (the extractor will typically be stateful in the latter case). 060 * @throws SQLException if an SQLException is encountered getting column 061 * values or navigating (that is, there's no need to catch SQLException) 062 * @throws DataAccessException in case of custom exceptions 063 */ 064 @Nullable 065 T extractData(ResultSet rs) throws SQLException, DataAccessException; 066 067}