Reputation: 809
I'm using the default ResultSet
when I do a JDBC call. I'm surprised that after using next()
to iterate the ResultSet
, I could call first()
to back to the first row. This is not meant by using forward only ResultSet
?
My code is quite simple:
Statement st = conn.createStatement();
rs = st.executeQuery(sql);
while (rs.next()) {
for (int i = 1; i <= columnsNumber; i++) {
if (i > 1) System.out.print(", ");
String columnValue = rs.getString(i);
System.out.print(rsmd.getColumnName(i) + " : " + columnValue);
}
System.out.println("");
}
rs.first();
I am using 8.0.11 version of mysql connector
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.11</version>
</dependency>
Upvotes: 1
Views: 2749
Reputation: 123654
The default behaviour for MySQL Connector/J is to load the entire contents of the ResultSet
into memory as soon as .executeQuery
is called. So, even though our ResultSet
is TYPE_FORWARD_ONLY
the MySQL JDBC developers apparently decided to be "nice" and allow us to use .first
, .absolute
, etc. in that case (because the entire ResultSet
is in memory and readily available), even though the JDBC spec says
For a
ResultSet
object that is of typeTYPE_FORWARD_ONLY
, the only valid cursor movement method isnext
. All other cursor movement methods throw anSQLException
.
Note, however, that if the entire ResultSet
is not guaranteed to be in memory, e.g., if we use st.setFetchSize(Integer.MIN_VALUE)
to "stream" the ResultSet
as we scroll through it, then MySQL Connector/J won't let us use anything but .next
or we'll get
com.mysql.jdbc.OperationNotSupportedException: Operation not supported for streaming result sets
Upvotes: 3