分享
 
 
 

数据库连接之jtds介绍(ResultSet is read only)

王朝other·作者佚名  2006-10-26
窄屏简体版  字體: |||超大  

欲流远之,必固其源泉,虽然现在有了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/updateable ResultSet.

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 calling executeQuery() you'll get a warning about the ResultSet being downgraded. Also, please take a look at our ResultSet 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).

JDBC Type

SQL Server Cursor Type

Server Load

Description

TYPE_FORWARD_ONLY

Firehose cursor (direct select) when read-only

Light

Fast, but driver will have to read all data. Not recommended when using multiple result sets. Forward only.

Fast forward-only (static) cursor when read-only and useCursors=true

Heavy

Slower than firehose cursors (multiple fetch requests), driver doesn't have to read all data. Forward only.

Forward-only dynamic cursor when updatable

Heavy

Others' updates, deletes and inserts visible. Forward only.

TYPE_SCROLL_INSENSITIVE

Static cursor

Heavy

Only 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_SENSITIVE

Keyset cursor

Medium

Others' updates or deletes visible, but not others' inserts. Scrollable.

TYPE_SCROLL_SENSITIVE+1

Dynamic cursor

Heavy

Others' updates, deletes and inserts visible. Scrollable.

jTDS supports the following result set concurrencies on MS SQL Server.

JDBC Concurrency

SQL Server Concurrency

Row Locks

Description

CONCUR_READ_ONLY

Read only

No

Read-only.

CONCUR_UPDATABLE

Optimistic concurrency, updatable

No

Row integrity checked using timestamp comparison or, when not available, value comparison (except text and image fields).

CONCUR_UPDATABLE+1

Pessimistic concurrency, updatable

Yes

Row integrity is ensured by locking rows.

CONCUR_UPDATABLE+2

Optimistic concurrency, updatable

No

Row integrity checked using value comparison (except text and image fields).

在这里,我们可以发现一点小小的变化,那就是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文档。

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有