<p>在查询到结果集的之后,可以直接通过修改结果集而是数据库中的记录同时发生变化,方法:把结果集设置成可更新结果集。不推荐这样做。如果想更新数据库记录,最要显性调用update语句。</p>
<p>代码如下:</p>
<p> <textarea cols="74" rows="15" name="code" class="java">static void read() throws SQLException, InterruptedException {
Connection conn = null;
Statement st = null;
ResultSet rs = null;
try {
// 2.建立连接
conn = JdbcUtils.getConnection();
// conn = JdbcUtilsSing.getInstance().getConnection();
// 3.创建语句
st = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_UPDATABLE);
// 4.执行语句
rs = st
.executeQuery("select id, name, money, birthday from user where id < 5");
// 5.处理结果
while (rs.next()) {
int id = rs.getInt("id");
System.out.println("show " + id + "...");
Thread.sleep(10000);
System.out.println(id + "\t" + rs.getObject("name") + "\t"
+ rs.getObject("birthday") + "\t"
+ rs.getObject("money"));
}
} finally {
JdbcUtils.free(rs, st, conn);
}
}
</textarea></p>
<p>ResultSet.TYPE_SCROLL_SENSITIVE和ResultSet.TYPE_SCROLL_INSENSITIVE的区别,都是可滚动的。但是ResultSet.TYPE_SCROLL_SENSITIVE对查询结束之后的数据库更新敏感,ResultSet.TYPE_SCROLL_INSENSITIVE不敏感。这也跟jdbc的驱动有关。mysql数据库没有实现对数据更新敏感。</p>
<p>代码如下:</p>
<p> <textarea cols="74" rows="15" name="code" class="java">static void read() throws SQLException, InterruptedException {
Connection conn = null;
Statement st = null;
ResultSet rs = null;
try {
// 2.建立连接
conn = JdbcUtils.getConnection();
// conn = JdbcUtilsSing.getInstance().getConnection();
// 3.创建语句
st = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_UPDATABLE);
// 4.执行语句
rs = st
.executeQuery("select id, name, money, birthday from user where id < 5");
// 5.处理结果
while (rs.next()) {
int id = rs.getInt("id");
System.out.println("show " + id + "...");
Thread.sleep(10000);
System.out.println(id + "\t" + rs.getObject("name") + "\t"
+ rs.getObject("birthday") + "\t"
+ rs.getObject("money"));
}
} finally {
JdbcUtils.free(rs, st, conn);
}
}
</textarea></p>
<p>ResultSet.TYPE_SCROLL_SENSITIVE和ResultSet.TYPE_SCROLL_INSENSITIVE的区别,都是可滚动的。但是ResultSet.TYPE_SCROLL_SENSITIVE对查询结束之后的数据库更新敏感,ResultSet.TYPE_SCROLL_INSENSITIVE不敏感。这也跟jdbc的驱动有关。mysql数据库没有实现对数据更新敏感。</p>