JDBC 可更新和对更新敏感的结果集

public class OtherApi {

    /**
     * @param args
     * @throws SQLException
     * @throws InterruptedException
     */
    public static void main(String[] args) throws SQLException,
            InterruptedException {
        read();
    }

    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,//第一个参数表示可滚动,INSENSITIVE表示对更新不敏感,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);//在这十秒钟手动修改下一条数据,如果数据库引擎支持更新敏感,那么结果集也会重新去查询数据库取得最新数据,由于Mysql不支持更新敏感,所以上面第二个参数没有起效果
                System.out.println(id + " " + rs.getObject("name") + " "
                        + rs.getObject("birthday") + " "
                        + rs.getObject("money"));
               
                if("1".equals(id)) {
                    rs.updateFloat("money", 300f);//上面的第二个参数,导致这个地方可以更新到数据库里
                    rs.updateRow();
                }
            }
        } finally {
            JdbcUtils.free(rs, st, conn);
        }
    }

}

原文地址:https://www.cnblogs.com/flying607/p/3462547.html