为何出现这个错误:java.sql.SQLException: 对只读结果集的无效操作: updateString;code如下:

bellfeel 2002-05-14 09:44:25
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
Connection con = DriverManager.getConnection("jdbc:oracle:thin:@team1-server:1521:hims","eharbor","eharbor");

// SQL Request

//Statement stmt = con.createStatement(ResultSet.TYPE_FORWARD_ONLY ,ResultSet.CONCUR_UPDATABLE);
Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
ResultSet rs = null;
rs = stmt.executeQuery("SELECT * FROM t_test WHERE file_ID='0001'");
if(rs.next()){
rs.updateString("file_name","davidtest");
rs.updateRow();
}
...全文
697 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
jackyhj 2002-05-27
  • 打赏
  • 举报
回复
这个问题在java api文档中有过解释,现提供如下:
With the addition of new functionality in the JDBC 2.0 API, it is possible for an application to request features that a DBMS or driver do not support. If the driver does not support scrollable result sets, for example, it may return a forward-only result set. Also, some queries will return a result set that cannot be updated, so requesting an updatable result set would have no effect for those queries. A general rule is that a query should include the primary key as one of the columns it selects, and it should reference only one table.

New methods in the JDBC 2.0 API let an application discover which result set features a driver supports. If there is any doubt about whether a feature is supported, it is advisable to call these methods before requesting the feature. The following DatabaseMetaData methods indicate whether a driver supports a given result set type or a given result set concurrency:

DatabaseMetaData.supportsResultSetType - returns a boolean indicating whether the driver supports the given result set type
DatabaseMetaData.supportsResultSetConcurrency - returns a boolean indicating whether the driver supports the given concurrency type in combination with the given result set type
The following ResultSet methods return the result set type and result set concurrency for the particular result set on which the method is called:

ResultSet.getType - returns the type of this result set
ResultSet.getConcurrency - returns the concurrency mode of this result set
If an application specifies a scrollable result set and the driver does not support scrolling, the driver will issue a warning on the Connection object that produced the statement and return a result set that is forward-only. Even if the driver supports scrollable result sets, it is possible for an application to request a scrollable type that the driver does not support. In such a case, the driver will issue an SQLWarning on the Connection object that produced the statement and return a scrollable result set of a type that it does support, even if it differs from the exact type requested. For example, if an application requests a TYPE_SCROLL_SENSITIVE result set and the driver does not support that type, it could return a TYPE_SCROLL_INSENSITIVE result set if it supports that type. The driver would also alert the application that it did not return the exact type requested by issuing an SQLWarning on the Connection object that produced the statement requesting the unsupported result set type.

Similarly, if an application specifies an updatable result set, a driver that does not support updatable result sets will issue an SQLWarning on the Connection object that produced the statement and return a read-only result set. If the application requests both an unsupported result set type and an unsupported concurrency type, the driver should choose the result set type first.

In some situations, a driver may need to choose an alternate result set type or concurrency type at statement execution time. For example, a SELECT statement that contains a join over multiple tables might produce a result set that is not updatable. In such a situation, the driver will issue an SQLWarning on the Statement, PreparedStatement, or CallableStatement object that tried to create the result set instead of issuing it on the Connection object. The driver will then choose an appropriate result set type and/or concurrency type according to the guidelines in the preceding two paragraphs.
问题就是由于数据库驱动的问题,你可以通过rs.getConcurrency()查看当前记录集的游标类型,如果是ResultSet.CONCUR_UPDATABLE,表示可以做更新,但如果是ResultSet.CONCUR_READ_ONLY,就表示不能做更改数据库操作。
明白了么?
huangzt 2002-05-27
  • 打赏
  • 举报
回复
解决的方法是:

不要用select * ...作查询,
而要用select <直接写出所有字段>的方式.
huangzt 2002-05-27
  • 打赏
  • 举报
回复
可怜的人啊,
想当年我也遇到相同的问题,
往事不堪回首,

skyyoung 2002-05-17
  • 打赏
  • 举报
回复
Statement stmt = con.createStatement(ResultSet.TYPE_FORWARD_ONLY ,ResultSet.CONCUR_UPDATABLE);

if(rs.next()){
rs.updateString("file_name","davidtest");
}
rs.updateRow();

这样行吗?

---------------
shmilu@sina.com
jackyhj 2002-05-17
  • 打赏
  • 举报
回复
我用oracle试过了,是有这样的错误,应该是数据库驱动的问题,可以换换其他的驱动试试看,因为我现在用的驱动是瘦客户机的驱动连接
bellfeel 2002-05-14
  • 打赏
  • 举报
回复
为何没人回答我的问题呀?
我用mysql测试可以通过,但在oracle下却出现此问题,是数据库的问题??
bellfeel 2002-05-14
  • 打赏
  • 举报
回复
我是用来上载文件用的,先保存文本数据,再保存blob数据呀,请教了,各位。
jackyhj 2002-05-14
  • 打赏
  • 举报
回复
为什么一定要这样做,直接用update语句不就可以了么
jackyhj 2002-05-14
  • 打赏
  • 举报
回复
它已经改过来了,不是注释了么!
worldheart 2002-05-14
  • 打赏
  • 举报
回复
这句话错了,
(ResultSet.TYPE_FORWARD_ONLY ,ResultSet.CONCUR_UPDATABLE);

需要改为:
(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);

bellfeel 2002-05-14
  • 打赏
  • 举报
回复
我用的是oracle8.17

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧