JDBC的ResultSet对象,一般用法中update和select是由独立的Statement来完成。
尝试了一下直接update结果集。数据库oracle9i,驱动classes12.jar
Connection con = ds.buildConnection();
PreparedStatement smt = con.prepareStatement(sql,ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
ResultSet rs = smt.executeQuery();
while(rs.next()){
rs.updateString("name","test"); rs.updateRow();
}
分别使用不同的sql:
String sql = "select a.name,b.id from user a,user_employee b where a.id = b.user_id";
这样的做法会抛出异常,rs这个时候是只读的。
String sql = "select a.name from user a";
单表操作的时候没有问题。
String sql = "select * from user"
用*的方式有问题,rs只读。
再尝试一种方式:
String sql = "select id+id1 mycol,name from user";
修改一下update语句:
rs.updateInt("aa",1);
这个时候,rs仍然保持是updatable,但是这句话却会引起一个异常:
java.sql.SQLException: ORA-01733: 此处不允许虚拟列
at oracle.jdbc.dbaccess.DBError.throwSqlException(DBError.java:134)
at oracle.jdbc.ttc7.TTIoer.processError(TTIoer.java:289)
。。。。。。
当然这一列通过dbmeta来获取也是readonly的。
总结一下,基本上,是单表的,非虚列,非通配符的statement可以被修改,而其他则不行。
只在jdk文档中找到了一段话是和ResultSet相关的:
A default ResultSet object is not updatable and has a cursor that moves forward only. Thus, you can iterate through it only once and only from the first row to the last row. It is possible to produce ResultSet objects that are scrollable and/or updatable. The following code fragment, in which con is a valid Connection object, illustrates how to make a result set that is scrollable and insensitive to updates by others, and that is updatable. See ResultSet fields for other options.
Statement stmt = con.createStatement(
ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_UPDATABLE);
ResultSet rs = stmt.executeQuery("SELECT a, b FROM TABLE2");
// rs will be scrollable, will not show changes made by others,
// and will be updatable
没有找到更加具体全面的说明,有篇文章
(http://www.zhuoda.org/blog3/article.jsp?blogid=1681&id=1833)说DB2 Everyplace不支
持CONCUR_UPDATABLE类型的结果集,还有很早以前用sybase的时候,sybase的游标类型支持也是有些
问题的,不知道哪里能找到相关方面的比较全面的文章,有谁知道,希望留在我的blog上,先谢了。