ResultSet.TYPE_SCROLL_SENSITIVE的行为 [英] Behaviour of ResultSet.TYPE_SCROLL_SENSITIVE
问题描述
我对类型为TYPE_SCROLL_SENSITIVE
的ResultSet
的行为感到困惑.
I am confused about the behaviour of a ResultSet
that is of type TYPE_SCROLL_SENSITIVE
.
我对此的理解是:
- 我执行一个选择查询,该查询返回一个结果集.我在第一行中打印出特定列的值.
- 然后我执行
Thread.sleep(10000)
,这将使程序暂停10秒钟. - 程序处于休眠状态时,我手动更新了数据库中的同一列(通过SQL提示符).
- 10秒后,我再次在结果集的第一行中打印同一列的值.
- I execute a select query that returns me a result set. I print out the value of a particular column in the first row.
- I then execute
Thread.sleep(10000)
, which halts the program for 10 seconds. - While the program is sleeping, I manually do an update to the same column in the DB (through the SQL prompt).
- After 10 seconds, I again print the value of the same column in the first row of the result set.
在第4步中,我希望打印的列值与在第1步中打印的值不同.但是,我总是得到相同的值(即使我的ResultSet
的类型为SCROLL_TYPE_SENSITIVE
).
In step 4, I expect the printed column value to be different from the value printed in step 1. But I always get the same value (even if my ResultSet
is of type SCROLL_TYPE_SENSITIVE
).
我在这里误会了什么吗?
Am I misunderstanding something here ?
下面是我使用的代码.
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()
.此外,请阅读
Yes. You must fetch again to get the latest state of the table, either by firing up a SELECT
yourself, or calling ResultSet.refreshRow()
. Moreover, read the docs of ResultSet.refreshRow()
before using it, otherwise you might get unexpected results.
文档中有关TYPE_SCROLL_SENSITIVE的信息,
The doc states regarding TYPE_SCROLL_SENSITIVE,
TYPE_SCROLL_SENSITIVE
TYPE_SCROLL_SENSITIVE
指示类型的常量 可滚动的ResultSet对象 并且通常对变化敏感 别人造的.
The constant indicating the type for a ResultSet object that is scrollable and generally sensitive to changes made by others.
这仅表示它将对同一ResultSet对象中其他人所做的更改敏感.为了理解这个概念,我建议您看一下这个官方的 JDBC教程:更新表.
Which merely means that it would be sensitive to the changes made by others in the same ResultSet object. To understand the concept, I would advise to look at this official JDBC Tutorial: Updating Tables.
好的,编辑我的帖子,使其包含原始教程中的特定行,
Okay, editing my post to include the specific line from the original tutorial,
通过可滚动的结果集,您可以 移至您要更改的行,并 如果类型是TYPE_SCROLL_SENSITIVE, 您可以连续获取新值 更改后.
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.
这篇关于ResultSet.TYPE_SCROLL_SENSITIVE的行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!