Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I'm going to go with Elijah's interpretation, that Nimesh actually meant, "Is it possible to make a wrapper Iterable for a ResultSet. Here's something I whipped up. It's obviously not yet production-ready, but it definitely gives the idea. Since there is no standard (as far as I know) ResultSetRow, it returns an Object[] for each row.</p> <p>There are some significant issues with this concept, not least that Iterator is not allowed to throw, so SQLExceptions are handled as best possible "on-site". Also, ResultSet is a very complex and featureful beast (see for instance the overloaded methods for every type). This complexity <em>could</em> be partially taken advantage of but isn't. For instance, there could be methods like <code>getIntIterable(ResultSet rs)</code> which returns a <code>Iterable&lt;int[]&gt;</code>. The method could check the <code>getColumnType</code>s of the ResultSetMetadata object. That way, it could throw (if for instance not all columns are ints) before creating the Iterable, the methods of which can not throw.</p> <pre><code>public static class ResultSetIterable implements Iterable&lt;Object[]&gt; { private ResultSet rs; private int columnCount; public ResultSetIterable(ResultSet rs) throws SQLException { this.rs = rs; columnCount = rs.getMetaData().getColumnCount(); } public Iterator&lt;Object[]&gt; iterator() { return new Iterator&lt;Object[]&gt;() { private boolean moreRows; { try { moreRows = rs.first(); } catch(SQLException e) { moreRows = false; } } public boolean hasNext() { boolean knownClosed = false; try { knownClosed = rs.isClosed(); } catch(Throwable e) { // Ignore possible SQLException or AbstractMethodError or... } return !knownClosed &amp;&amp; moreRows; } public Object[] next() { Object[] curRow = new Object[columnCount]; for(int i = 1; i &lt;= columnCount; i++) { try { curRow[i - 1] = rs.getObject(i); } catch(SQLException e) { curRow[i - 1] = null; } } try { moreRows = rs.next(); } catch(SQLException e) { moreRows = false; } return curRow; } public void remove() { try { rs.previous(); rs.deleteRow(); } catch(SQLException e) { throw new UnsupportedOperationException("ResultSetIterable does not support remove for this ResultSet type."); } } }; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload