I am working with SQLite and JDBC and getting this error about the result_set being TYPE_FORWARD_ONLY.
PreparedStatement get_mileage = conn.prepareStatement("SELECT * FROM workout_log");
ResultSet mileage_count = get_mileage.executeQuery();
mileage_count.absolute(week_start);
for (int i=week_start;i<=week_finish;i++){
total_mileage+=mileage_count.getInt(1);
mileage_count.next();
}
The error is on the call to absolute()
even though I know it is not moving backwards at all. I have tried adding flags to prepareStatement
but it says my version of SQLite does not support ResultSet
that is not FORWARD_ONLY.
My question is why is this occuring, even though I am not moving backward?
A
TYPE_FORWARD_ONLY ResultSet
only supportsnext()
for navigation, and not methods likefirst()
,last()
,absolute(int)
,relative(int)
. The JDBC specification explicitly defines those to throw aSQLException
if called on aTYPE_FORWARD_ONLY
:Javadoc of
ResultSet.absolute(int)
:Using those methods does not make a lot of sense with a
TYPE_FORWARD_ONLY
: that type of result set is not intended for 'random access' of the rows, like the scrollable result sets are.For example with a
TYPE_FORWARD_ONLY
:first()
would only work when you are positioned before the first row (and on the first row itself), so why not just usenext()
absolute(int)
would only work if you pass a row higher than the current row, and you could never go back to earlier rowsrelative(int)
would only work if you pass a positive value and you can never go back to earlier rowslast()
would make you skip the rest of the result set and you can never go back to earlier rowsAdmittedly: it might have its uses, but it would needlessly complicate the driver with the additional constraints of being
TYPE_FORWARD_ONLY
.If you want random access, you need to declare that you want random access by specifying one of the scrollability types
TYPE_SCROLL_INSENSITIVE
orTYPE_SCROLL_SENSITIVE
. If your driver doesn't support these types, then you may need to simulate it with for exampleCachedRowSet
(specificallycom.sun.rowset.CachedRowSetImpl
), or by first loading the entire ResultSet (eg into aList<? extends List<Object>>
).