ResultSet may only be accessed in a forward direction
来源:互联网 发布:淘宝新规店铺负责人 编辑:程序博客网 时间:2024/05/17 22:16
欲流远之,必固其源泉,虽然现在有了Hibernate等工具,或许我们没太多必要关注JDBC的底层操作,但是工具会不断更新或者过时或者被淘汰,所以Java基础还是最重要的,有了深厚的JDBC功底,相信再学其它ORM都轻车熟路,不费吹灰之力。
I get java.sql.SQLException: "ResultSet may only be accessed in a forward direction"
or"ResultSet is read only"
when using a scrollable/updateableResultSet
.
There are three possible causes to this (if we exclude not creating the ResultSet
with the appropriate type and concurrency in the first place):
- The executed query must be a single SELECT statement or a call to a procedure that consists of a single SELECT statement (even a SET or PRINT will cause the resulting ResultSet to be forward only read only). This is a SQL Server limitation and there's not much jTDS can do about it.
- The scroll insensitive/updateable combination is not supported by SQL Server, so such a
ResultSet
is automatically downgraded to scroll insensitive/read-only by the server. Use the scroll sensitive/updateable combination and it should work. - The other possible cause is that the cursor is keyset-based and either the table you are selecting from does not have a unique primary key or that primary key is not included in your
SELECT
. See the SQL Server Documentation on cursor types for more information.
In both cases if you call Statement.getWarnings()
right after callingexecuteQuery()
you'll get a warning about theResultSet
being downgraded. Also, please take a look at ourResultSet
support page for some additional information and tips.
JDK5支持用rs.updateRow()直接更新当前行,而我们习惯的用法是
PreparedStatement pstmt=conn.prepareStatement(sql,ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
pstmt.set.....
ResultSet rs=pstmt.executeQuery();
//更新操作
if(rs.next()){
rs.updateString("fieldName","value");
...
rs.updateRow()
}
当然,这在Microsoft的JDBC驱动里面是没有问题的,可是当你用jtds的时候,这种情况就发生变化了,且让我们来参考一下jTDS官方网上的介绍——jTDS supports the following result set types on MS SQL Server(http://jtds.sourceforge.net/resultSets.html).
useCursors=true
HeavySlower than firehose cursors (multiple fetch requests), driver doesn't have to read all data. Forward only.Forward-only dynamic cursor when updatableHeavyOthers' updates, deletes and inserts visible. Forward only.TYPE_SCROLL_INSENSITIVEStatic cursorHeavyOnly works with read-only concurrency (updatable is downgraded). SQL Server generates a temporary table, so changes made by others are not visible. Scrollable.TYPE_SCROLL_SENSITIVEKeyset cursorMediumOthers' updates or deletes visible, but not others' inserts. Scrollable.TYPE_SCROLL_SENSITIVE+1Dynamic cursorHeavyOthers' updates, deletes and inserts visible. Scrollable.jTDS supports the following result set concurrencies on MS SQL Server.
在这里,我们可以发现一点小小的变化,那就是jTDS的TYPE_SCROLL_INSENSITIVE只支持只读操作(Only works with read-only concurrency (updatable is downgraded)),TYPE_SCROLL_SENSITIVE支持Update操作,但不支持另外的Insert(说明:此Insert指的是新增一条空记录,并在当前记录中填值的情况),而TYPE_SCROLL_SENSITIVE+1就跟MS SQL Server JDBC驱动中的TYPE_SCROLL_INSENSITIVE功能类似了。
基于此,所以我们要将原来的语句:
pstmt=conn.prepareStatement(sql,ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
改成如下形式:
pstmt=conn.prepareStatement(sql,ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
这样,你就可以应用查询,并在结果集rs的当前行直接进行更新操作了,关于ResultSet的更新用法请参照JDK文档
- ResultSet may only be accessed in a forward direction
- ResultSet may only be accessed in a forward direction
- ResultSet may only be accessed in a forward direction 问题解决
- ResultSet may only be accessed in a forward direction
- ResultSet may only be accessed in a forward direction
- 解决 ResultSet may only be accessed in a forward direction 错误
- java.sql.SQLException: ResultSet may only be accessed in a forward direction.报错
- ResultSet may only be accessed in a forward direction Hibernate4异常解决方案
- SQL server 读取某表行数以及报错ResultSet may only be accessed in a forward direction
- A Static Method Should Be Accessed In A Static Way
- Spark Q&A : Only one SparkContext may be running in this JVM
- Only one SparkContext may be running in this JVM
- Recursion all file direction in a direction
- Java Warning:A Static Method Should Be Accessed In A Static Wa
- new types may not be defined in a return type
- a pointer to a bound function may only be used to call the function
- a pointer to a bound function may only be used to call the function
- Only MySqlParameter objects may be stored
- SQLProgress 1.01.36正式版发布
- sql server表中timestamp类型的具体说明
- java acm 基础知识
- 用popen函数操作其它程序的输入和输出
- oracle 10g OEM常规错误解决-------Unknown host specified
- ResultSet may only be accessed in a forward direction
- sql语句-linq语言-lambda表达式对照
- org.dom4j.DocumentException: www.apple.com Nested exception: www.apple.com
- 自学linux信号
- pthread_create()函数用法
- FTP命令解析
- linux修改ssh端口和禁止root远程登陆设置
- Comparator和Comparable在排序中的应用
- joomla模板、模块关系 | joomla网站建设第二贴