JDBC结果集Type_Scroll_Sensitive [英] JDBC ResultSet Type_Scroll_Sensitive

查看:140
本文介绍了JDBC结果集Type_Scroll_Sensitive的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用JDBC和不同类型的ResultSet,并对将其设置为TYPE_SCROLL_SENSITIVE有疑问(此问题与敏感部分有关,而不与类型滚动部分有关).从理论上讲,数据库中的更改会反映在ResultSet中,反之亦然,但这对我而言并不是这种情况.

这是我尝试过的事情:

Connection conn = null;
try {
    DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
    String url = "jdbc:oracle:thin:@oracle.myCompany.com:1521:xe";
    conn = DriverManager.getConnection(url, "username", "password");
    Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);

    String query = "SELECT user_id, first_name, last_name FROM Users";
    ResultSet rs = stmt.executeQuery(query);

    while(rs.next()){
        System.out.println("-----------------------------");
        System.out.println(rs.getInt(1));
        System.out.println(rs.getString("first_name"));
        System.out.println(rs.getString("last_name"));

        int id = rs.getInt(1);
        rs.updateInt(1,100+id);
    }

} catch (SQLException e) {
    e.printStackTrace();
} finally{
    conn.close();
}

当我更新ID时,将ID加100,则更改不会显示在数据库中(如果在此下面添加rs.updateRow(),则它会更新数据库.但是,如果更新ID,也会更新数据库)我将其设为TYPE_SCROLL_INSENSITIVE).

我尝试的另一件事是这样:

Connection conn = null;
try {
    DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
    String url = "jdbc:oracle:thin:@oracle.myCompany.com:1521:xe";
    conn = DriverManager.getConnection(url, "username", "password");
    Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);

    String query = "SELECT user_id, first_name, last_name FROM Users";
    ResultSet rs = stmt.executeQuery(query);

    String query2 = "INSERT INTO User VALUES (4, 'Joe', 'Bloggs')";
    stmt.executeUpdate(query2);

    while(rs.next()){
        System.out.println("-----------------------------");
        System.out.println(rs.getInt(1));
        System.out.println(rs.getString("first_name"));
        System.out.println(rs.getString("last_name"));

        int id = rs.getInt(1);
        rs.updateInt(1,100+id);
    }

} catch (SQLException e) {
    e.printStackTrace();
} finally{
    conn.close();
}

但是,如果我运行此代码,并希望新行位于resultSet中,因为它很敏感...我会在以下行中得到NullPointerException:

那么有人可以解释敏感的ResultSet如何工作以及我在做什么错吗?可能有一个例子吗?

解决方案

TYPE_SCROLL_SENSITIVE表示,如果在遍历表数据时对表进行了更改,则会看到该表.要重现此行为,请先中断

System.out.println("-----------------------------");

使用外部工具更改first_name,然后执行此行

System.out.println(rs.getString("first_name"));

更改应该是可见的.

请注意,此功能是可选的.要测试是否已启用,请运行

boolean res = conn.getMetaData().ownDeletesAreVisible(ResultSet.TYPE_SCROLL_SENSITIVE);
System.out.println(res);

I have been playing with JDBC and the different types of ResultSet and have a question about making it TYPE_SCROLL_SENSITIVE (this question is to do with the sensitive part, not the type scroll part). The theory says changes in the database are reflected in the ResultSet and vice-versa but this is not happening for me.

Here is something I've tried:

Connection conn = null;
try {
    DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
    String url = "jdbc:oracle:thin:@oracle.myCompany.com:1521:xe";
    conn = DriverManager.getConnection(url, "username", "password");
    Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);

    String query = "SELECT user_id, first_name, last_name FROM Users";
    ResultSet rs = stmt.executeQuery(query);

    while(rs.next()){
        System.out.println("-----------------------------");
        System.out.println(rs.getInt(1));
        System.out.println(rs.getString("first_name"));
        System.out.println(rs.getString("last_name"));

        int id = rs.getInt(1);
        rs.updateInt(1,100+id);
    }

} catch (SQLException e) {
    e.printStackTrace();
} finally{
    conn.close();
}

When I update the ID, adding 100 to it, the change isn't shown in the database (if, below this, I add rs.updateRow() then it does update the database. But this also updates the database if I make it TYPE_SCROLL_INSENSITIVE).

Another thing I tried was this:

Connection conn = null;
try {
    DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
    String url = "jdbc:oracle:thin:@oracle.myCompany.com:1521:xe";
    conn = DriverManager.getConnection(url, "username", "password");
    Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);

    String query = "SELECT user_id, first_name, last_name FROM Users";
    ResultSet rs = stmt.executeQuery(query);

    String query2 = "INSERT INTO User VALUES (4, 'Joe', 'Bloggs')";
    stmt.executeUpdate(query2);

    while(rs.next()){
        System.out.println("-----------------------------");
        System.out.println(rs.getInt(1));
        System.out.println(rs.getString("first_name"));
        System.out.println(rs.getString("last_name"));

        int id = rs.getInt(1);
        rs.updateInt(1,100+id);
    }

} catch (SQLException e) {
    e.printStackTrace();
} finally{
    conn.close();
}

But if I run this code, and expect the new row to be in the resultSet because it's sensitive... I get a NullPointerException on the line: while(rs.next()){

So could someone please explain how the sensitive ResultSet works and what I am doing wrong? Possibly with an example?

解决方案

TYPE_SCROLL_SENSITIVE means that if a change happened to the table while you are iterating over its data, you will see it. To reproduce this behavior make a break on

System.out.println("-----------------------------");

change the first_name using an external tool, then execute this line

System.out.println(rs.getString("first_name"));

the change should be visible.

Note that this feature is optional. To test if it's enabled run this

boolean res = conn.getMetaData().ownDeletesAreVisible(ResultSet.TYPE_SCROLL_SENSITIVE);
System.out.println(res);

这篇关于JDBC结果集Type_Scroll_Sensitive的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆