"TYPE_FORWARD_ONLY"错误;在sqlite中 [英] Error with "TYPE_FORWARD_ONLY" in sqlite
问题描述
我正在使用SQLite和JDBC,并收到有关result_set为TYPE_FORWARD_ONLY的错误.
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();
}
即使我不知道它根本没有向后移动,该错误仍在调用absolute()
时出现.我尝试将标志添加到prepareStatement
,但是它说我的SQLite版本不支持不是FORWARD_ONLY的ResultSet
.
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?
推荐答案
TYPE_FORWARD_ONLY ResultSet
仅支持next()
进行导航,而不支持first()
,last()
,absolute(int)
,relative(int)
之类的方法. JDBC规范明确定义了在TYPE_FORWARD_ONLY
上调用时抛出SQLException
的那些:
A TYPE_FORWARD_ONLY ResultSet
only supports next()
for navigation, and not methods like first()
, last()
, absolute(int)
, relative(int)
. The JDBC specification explicitly defines those to throw a SQLException
if called on a TYPE_FORWARD_ONLY
:
抛出:
SQLException
-如果发生数据库访问错误;在封闭的结果集上调用此方法,或者结果集类型为TYPE_FORWARD_ONLY
Throws:
SQLException
- if a database access error occurs; this method is called on a closed result set or the result set type isTYPE_FORWARD_ONLY
对于TYPE_FORWARD_ONLY
,使用这些方法没有多大意义:类似于可滚动的结果集,这种类型的结果集不适合行的随机访问".
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.
例如,使用TYPE_FORWARD_ONLY
:
- 仅当您位于第一行之前(并且位于第一行本身)时,调用
first()
才有效,所以为什么不只使用next()
- 仅当您传递的行比当前行高时调用
absolute(int)
才有效,并且您再也无法返回到较早的行 - 仅当您传递正值并且永远无法返回到较早的行时,调用
relative(int)
才有效 - 调用
last()
将使您跳过结果集的其余部分,并且永远无法返回到较早的行
- Calling
first()
would only work when you are positioned before the first row (and on the first row itself), so why not just usenext()
- Calling
absolute(int)
would only work if you pass a row higher than the current row, and you could never go back to earlier rows - Calling
relative(int)
would only work if you pass a positive value and you can never go back to earlier rows - Calling
last()
would make you skip the rest of the result set and you can never go back to earlier rows
诚然:它可能有其用途,但不必要地使驱动程序复杂化为TYPE_FORWARD_ONLY
的其他约束.
Admittedly: it might have its uses, but it would needlessly complicate the driver with the additional constraints of being TYPE_FORWARD_ONLY
.
如果要随机访问,则需要通过指定滚动性类型TYPE_SCROLL_INSENSITIVE
或TYPE_SCROLL_SENSITIVE
之一来声明要随机访问.如果您的驱动程序不支持这些类型,则可能需要使用例如CachedRowSet
(特别是com.sun.rowset.CachedRowSetImpl
)或首先加载整个ResultSet(例如,放入List<? extends List<Object>>
)对其进行仿真.
If you want random access, you need to declare that you want random access by specifying one of the scrollability types TYPE_SCROLL_INSENSITIVE
or TYPE_SCROLL_SENSITIVE
. If your driver doesn't support these types, then you may need to simulate it with for example CachedRowSet
(specifically com.sun.rowset.CachedRowSetImpl
), or by first loading the entire ResultSet (eg into a List<? extends List<Object>>
).
这篇关于"TYPE_FORWARD_ONLY"错误;在sqlite中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!