package jdbc; import java.io.Closeable; import java.io.IOException; import java.sql.ResultSet; import java.sql.SQLException; import java.util.Iterator; /** * A Closable iterator over a ResultSet. Returns the first column as an object **/ public class ResultSetIterator<T> implements Iterator<T>, Closeable { private ResultSet resultSet; public ResultSetIterator(ResultSet resultSet) { this.resultSet = resultSet; } @Override public boolean hasNext() { try { return resultSet.next(); } catch (SQLException e) { throw new RuntimeException(e); } } @Override public T next() { try { return (T) resultSet.getObject(1); } catch (SQLException e) { throw new RuntimeException(e); } } @Override public void close() throws IOException { try { resultSet.close(); } catch (SQLException e) { throw new IOException(e); } } @Override public void remove() { throw new UnsupportedOperationException(); } }