个人认为RESULTSET使用非常不灵活,如果做增加删除并考虑到事务问题,是否应该考虑用ejb有一个cacheRowset,好像还灵活一点,http://developer.java.sun.com/developer/technicalArticles/javaserverpages/cachedrowset/  

解决方案 »

  1.   

    1.
    要结果集具有回滚功能,创建的statement应该为:
    Statement st = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, CONCUR_READ_ONLY);
    第一个参数表示结果集是否回滚,第二个参数表示结果集的并发操作性,即是否通过结果集来改变数据库记录。上面的调用表示生成的结果集可以回滚,但此时的结果集与数据库脱钩,即使别的程序更新了数据库,但不反映到你的结果集中,如果要让其反映出来,即让你的结果集是适时更新的,则第一个参数应该是:ResultSet.TYPE_SCROLL_SENSITIVE;关于第二个参数,很容易顾名思义,另一种是ResultSet.CONCUR_UPDATABLE2.在结果集中可以进行很多操作,具体看一下文档,很明白。一般是从中取值,如果结果集是可更新的,则还可以进行插入记录,删除记录,更新纪录等操作。3.我没有碰到过,不知道,主要是我对数据库不熟悉,不敢乱说。4.在跟数据库操作时,有基础都可能出现空指针,主要表现在以下几方面:数据库连接没有建立,此时connection为null,造成原因可能有:没有找到驱动程序;建立连接的参数错误;结果集本身就是null,调用了next()方法,此时也会跑出空指针异常;去数据库字段的值时,数据库字段本身没有值,调用了该值类型相应的操作,最常见的是取出一个字符串没有判断是否为null而直接进行去除空格操作;还有就是在关闭数据库时,没有对结果集,执行语句,以及数据库连接进行null的判断,直接调用close()方法。如果前面的各部分操作是正常的,本来该处是不用null判断的,但可能就是前面的某一处出现错误,比如数据库连接没有建立,而关闭时调用conn.close(),就会出现空指针。估计还有其他的,暂时没有想到,较少遇到吧。敲得好累啊。哎
      

  2.   

    ResultSet.TYPE_FORWORD_ONLY
    ResultSet.SCROLL_SENSITIVE-------结果集是完全可浏览的,只要亦更新立刻就能在结果集中反映
    ResultSet.SCROLL_INSENSITIVE -------结果集是完全可浏览的,但只有在结果集关闭后才能看到更新.为了看到结果集,要创建一个新的结果集.
      

  3.   

    有的jdbc驱动程序就是不支持游标回滚的。
      

  4.   

    同意wjmmml(笑着悲伤) 的观点,小弟用的是Oracle8i,用Java.sql.ResulSet就不支持回滚功能,而Oracle自带的ResultSet则可以实现回滚,但功能很滥,象排序的功能就没有。
      

  5.   

    最好用jdbc第四类驱动,针对你的数据库下载一个驱动程序。
    非常好用。
    驱动程序不行,是无法实现回滚的。
      

  6.   

    createStatement
    public Statement createStatement(int resultSetType,
                                     int resultSetConcurrency,
                                     int resultSetHoldability)
                              throws SQLExceptionCreates a Statement object that will generate ResultSet objects with the given type, concurrency, and holdability. This method is the same as the createStatement method above, but it allows the default result set type, concurrency, and holdability to be overridden. Parameters:
    resultSetType - one of the following ResultSet constants: ResultSet.TYPE_FORWARD_ONLY, ResultSet.TYPE_SCROLL_INSENSITIVE, or ResultSet.TYPE_SCROLL_SENSITIVE
    resultSetConcurrency - one of the following ResultSet constants: ResultSet.CONCUR_READ_ONLY or ResultSet.CONCUR_UPDATABLE
    resultSetHoldability - one of the following ResultSet constants: ResultSet.HOLD_CURSORS_OVER_COMMIT or ResultSet.CLOSE_CURSORS_AT_COMMIT 
    Returns:
    a new Statement object that will generate ResultSet objects with the given type, concurrency, and holdability 
    Throws: 
    SQLException - if a database access error occurs or the given parameters are not ResultSet constants indicating type, concurrency, and holdability
    Since:
    1.4 
    See Also:
    ResultSet
      

  7.   

    这个问题我知道
    建Statement时用:
    Statement stmt=conn.createStatement(
                   ResultSet.TYPE_SCROLL_INSENSITIVE,
                   ResultSet.CONCUR_UPDATABLE);
    然后ResultSet rs=stmt.executeQuery("select ... from ...");
    这时rs就可回滚了我只用过  boolean absolute(int row) 纪录集定位到第某条
              void    beforeFirst()     移到第一条纪录前
    两个方法,大概也就够了注意,使用这个有可能产生一些奇怪的乱码