java – ResultSet.TYPE_SCROLL_SENSITIVE的行为

我对TYPE_SCROLL_SENSITIVE类型的ResultSet的行为感到困惑.

我对此的理解是:

>我执行一个选择查询,返回结果集.我打印出第一行中特定列的值.
>然后我执行Thread.sleep(10000),暂停程序10秒.
>当程序处于休眠状态时,我手动对数据库中的同一列进行更新(通过sql提示符).
> 10秒后,我再次打印结果集第一行中相同列的值.

在步骤4中,我希望打印的列值与步骤1中打印的值不同.但我总是得到相同的值(即使我的ResultSet的类型为SCROLL_TYPE_SENSITIVE).

在这里误解了什么吗?

以下是我使用的代码.

private void doStuff() throws Exception
{
    final String query = "select * from suppliers where sup_id=420";

    Statement stmt = this.con.createStatement(
        ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);

    ResultSet rs = stmt.executeQuery(query);

    rs.next();

    System.out.println("City : " + rs.getString("city"));

    Thread.sleep(10000); // While this executes,I do a manual update !

    System.out.println("City : " + rs.getString("city"));
}

解决方法

Am I mis-understanding something here ?

是.您必须再次获取获取表的最新状态,方法是自己启动SELECT或调用ResultSet.refreshRow().此外,在使用它之前阅读ResultSet.refreshRow()的文档,否则您可能会得到意想不到的结果.

关于TYPE_SCROLL_SENSITIVE的doc状态,

TYPE_SCROLL_SENSITIVE

The constant indicating the type for a
ResultSet object that is scrollable
and generally sensitive to changes
made by others.

这仅仅意味着它对同一ResultSet对象中其他人所做的更改很敏感.为了理解这个概念,我建议你看看这个官方的JDBC Tutorial: Updating Tables.

好的,编辑我的帖子以包含原始教程中的特定行,

With a scrollable result set,you can move to rows you want to change,and if the type is TYPE_SCROLL_SENSITIVE,you can get the new value in a row after you have changed it.

相关文章

最近看了一下学习资料,感觉进制转换其实还是挺有意思的,尤...
/*HashSet 基本操作 * --set:元素是无序的,存入和取出顺序不...
/*list 基本操作 * * List a=new List(); * 增 * a.add(inde...
/* * 内部类 * */ 1 class OutClass{ 2 //定义外部类的成员变...
集合的操作Iterator、Collection、Set和HashSet关系Iterator...
接口中常量的修饰关键字:public,static,final(常量)函数...