Package io.github.connellite.jdbc
Class ResultSetBeanIterator<T>
java.lang.Object
io.github.connellite.jdbc.ResultSetBeanIterator<T>
- Type Parameters:
T- bean type
- All Implemented Interfaces:
AutoCloseable,Iterator<T>
Forward-only iterator over JDBC query rows mapped to a simple POJO via
SimpleResultSetBeanMapper.
Column labels are validated against the result set metadata using ResultSetMetaDataUtils.
Use asIterable() for enhanced for-loops together with try-with-resources; the cursor is
single-pass.
Closing releases the result set and statement from construction.
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionExecutes a boundNamedPreparedStatementand iterates rows using provided mapper.ResultSetBeanIterator(SimpleResultSetBeanMapper<T> mapper, Connection conn, String query, Object... params) Executesqueryand iterates rows using provided mapper.ResultSetBeanIterator(Class<T> beanClass, NamedPreparedStatement nps) Executes a boundNamedPreparedStatementand iterates rows mapped tobeanClass.ResultSetBeanIterator(Class<T> beanClass, Connection conn, String query, Object... params) Executesqueryand iterates rows mapped tobeanClass.ResultSetBeanIterator(ResultSet resultSet, SimpleResultSetBeanMapper<T> mapper) Wraps an already openedResultSetand maps rows using provided mapper.ResultSetBeanIterator(ResultSet resultSet, Class<T> beanClass) Wraps an already openedResultSetand maps rows tobeanClass. -
Method Summary
Modifier and TypeMethodDescriptionSingle-passIterableview for enhanced for-loops.voidclose()Closes underlyingResultSet.static <T> List<T>getAll(SimpleResultSetBeanMapper<T> mapper, NamedPreparedStatement nps) Reads all mapped rows into an immutable list using provided mapper.static <T> List<T>getAll(SimpleResultSetBeanMapper<T> mapper, Connection conn, String query, Object... params) Reads all mapped rows into an immutable list using provided mapper.static <T> List<T>getAll(Class<T> beanClass, NamedPreparedStatement nps) Reads all mapped rows into an immutable list.static <T> List<T>getAll(Class<T> beanClass, Connection conn, String query, Object... params) Reads all mapped rows into an immutable list.static <T> List<T>getAll(ResultSet resultSet, SimpleResultSetBeanMapper<T> mapper) Reads all mapped rows from an existingResultSetinto an immutable list using provided mapper.static <T> List<T>Reads all mapped rows from an existingResultSetinto an immutable list.static <T> Optional<T>getFirst(SimpleResultSetBeanMapper<T> mapper, NamedPreparedStatement nps) Reads the first mapped row, if present, using provided mapper.static <T> Optional<T>getFirst(SimpleResultSetBeanMapper<T> mapper, Connection conn, String query, Object... params) Reads the first mapped row, if present, using provided mapper.static <T> Optional<T>getFirst(Class<T> beanClass, NamedPreparedStatement nps) Reads the first mapped row, if present.static <T> Optional<T>getFirst(Class<T> beanClass, Connection conn, String query, Object... params) Reads the first mapped row, if present.static <T> Optional<T>getFirst(ResultSet resultSet, SimpleResultSetBeanMapper<T> mapper) Reads the first mapped row from an existingResultSet, if present, using provided mapper.static <T> Optional<T>Reads the first mapped row from an existingResultSet, if present.booleanhasNext()next()Returns the current mapped bean and advances cursor.<C> ResultSetBeanIterator<T>withConverter(Class<C> type, TypeConverter<C> converter) Registers custom type converter in the underlying mapper and returns this iterator.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface java.util.Iterator
forEachRemaining, remove
-
Constructor Details
-
ResultSetBeanIterator
public ResultSetBeanIterator(Class<T> beanClass, Connection conn, String query, Object... params) throws SQLException Executesqueryand iterates rows mapped tobeanClass.- Throws:
SQLException
-
ResultSetBeanIterator
Executes a boundNamedPreparedStatementand iterates rows mapped tobeanClass.- Throws:
SQLException
-
ResultSetBeanIterator
public ResultSetBeanIterator(SimpleResultSetBeanMapper<T> mapper, Connection conn, String query, Object... params) throws SQLException Executesqueryand iterates rows using provided mapper.- Throws:
SQLException
-
ResultSetBeanIterator
public ResultSetBeanIterator(SimpleResultSetBeanMapper<T> mapper, NamedPreparedStatement nps) throws SQLException Executes a boundNamedPreparedStatementand iterates rows using provided mapper.- Throws:
SQLException
-
ResultSetBeanIterator
Wraps an already openedResultSetand maps rows tobeanClass.- Throws:
SQLException
-
ResultSetBeanIterator
public ResultSetBeanIterator(ResultSet resultSet, SimpleResultSetBeanMapper<T> mapper) throws SQLException Wraps an already openedResultSetand maps rows using provided mapper.- Throws:
SQLException
-
-
Method Details
-
withConverter
Registers custom type converter in the underlying mapper and returns this iterator. -
asIterable
Single-passIterableview for enhanced for-loops.Iterable.iterator()returnsthis; one traversal consumes theResultSet. -
hasNext
public boolean hasNext() -
next
Returns the current mapped bean and advances cursor. -
close
Closes underlyingResultSet.- Specified by:
closein interfaceAutoCloseable- Throws:
Exception
-
getAll
public static <T> List<T> getAll(Class<T> beanClass, Connection conn, String query, Object... params) throws SQLException Reads all mapped rows into an immutable list.- Throws:
SQLException
-
getAll
public static <T> List<T> getAll(Class<T> beanClass, NamedPreparedStatement nps) throws SQLException Reads all mapped rows into an immutable list.- Throws:
SQLException
-
getAll
public static <T> List<T> getAll(SimpleResultSetBeanMapper<T> mapper, Connection conn, String query, Object... params) throws SQLException Reads all mapped rows into an immutable list using provided mapper.- Throws:
SQLException
-
getAll
public static <T> List<T> getAll(SimpleResultSetBeanMapper<T> mapper, NamedPreparedStatement nps) throws SQLException Reads all mapped rows into an immutable list using provided mapper.- Throws:
SQLException
-
getFirst
public static <T> Optional<T> getFirst(Class<T> beanClass, Connection conn, String query, Object... params) throws SQLException Reads the first mapped row, if present.- Throws:
SQLException
-
getFirst
public static <T> Optional<T> getFirst(Class<T> beanClass, NamedPreparedStatement nps) throws SQLException Reads the first mapped row, if present.- Throws:
SQLException
-
getFirst
public static <T> Optional<T> getFirst(SimpleResultSetBeanMapper<T> mapper, Connection conn, String query, Object... params) throws SQLException Reads the first mapped row, if present, using provided mapper.- Throws:
SQLException
-
getFirst
public static <T> Optional<T> getFirst(SimpleResultSetBeanMapper<T> mapper, NamedPreparedStatement nps) throws SQLException Reads the first mapped row, if present, using provided mapper.- Throws:
SQLException
-
getAll
Reads all mapped rows from an existingResultSetinto an immutable list.- Throws:
SQLException
-
getAll
public static <T> List<T> getAll(ResultSet resultSet, SimpleResultSetBeanMapper<T> mapper) throws SQLException Reads all mapped rows from an existingResultSetinto an immutable list using provided mapper.- Throws:
SQLException
-
getFirst
Reads the first mapped row from an existingResultSet, if present.- Throws:
SQLException
-
getFirst
public static <T> Optional<T> getFirst(ResultSet resultSet, SimpleResultSetBeanMapper<T> mapper) throws SQLException Reads the first mapped row from an existingResultSet, if present, using provided mapper.- Throws:
SQLException
-