User27854
User27854

Reputation: 884

Trying to understanding the functionality of createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);

I have written a small program to understand the functionality of the above function actually. Description of the function says the following, Creates a Statement object that will generate ResultSet objects with the given type and concurrency. To test this I have written a small program which displays the contents of a table in database. and then I display it repeatedly, after sometime delay. Meanwhile I modify/or change the contents in the Database. I was actually expecting that to be displayed on the output screen. but nothing like that happened.

Can some one give me a hint regarding this. below is the code snipped and the output..

String sql= "select * from customer";
  st=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);

    rs=st.executeQuery(sql);

    for(int j=1;j<21;j++){
        System.out.println("Count is "+j);

    while(rs.next()){
        System.out.println(rs.getInt(1)+"\t"+rs.getString(2)+"\t"+rs.getString(3)+"\t"+rs.getInt(4)+"\t"+rs.getInt(5));
        for(int i=0;i<1000000000l;i++);
    }

    while(rs.previous()){
        System.out.println(rs.getInt(1)+"\t"+rs.getString(2)+"\t"+rs.getString(3)+"\t"+rs.getInt(4)+"\t"+rs.getInt(5));
        for(int i=0;i<1000000000l;i++);
    }

    }

output

Count is 1

101 cust1 [email protected] 1111 1000

102 cust2 [email protected] 2222 2000

103 cust3 [email protected] 3333 3000

104 cust4 [email protected] 4444 4000

105 cust5 [email protected] 5555 5000

106 cust6 [email protected] 6666 6000

107 cust7 [email protected] 7777 7000

108 cust8 [email protected] 8888 8000

109 cust9 [email protected] 9999 9000

110 cust10 [email protected] 1010 10000

111 cust11 [email protected] 1011 11000

111 cust11 [email protected] 1011 11000

110 cust10 [email protected] 1010 10000

109 cust9 [email protected] 9999 9000

108 cust8 [email protected] 8888 8000

107 cust7 [email protected] 7777 7000

106 cust6 [email protected] 6666 6000

105 cust5 [email protected] 5555 5000

104 cust4 [email protected] 4444 4000

103 cust3 [email protected] 3333 3000

102 cust2 [email protected] 2222 2000

101 cust1 [email protected] 1111 1000

Count is 2

101 cust1 [email protected] 1111 1000

102 cust2 [email protected] 2222 2000

103 cust3 [email protected] 3333 3000

104 cust4 [email protected] 4444 4000

105 cust5 [email protected] 5555 5000

106 cust6 [email protected] 6666 6000

107 cust7 [email protected] 7777 7000

108 cust8 [email protected] 8888 8000

109 cust9 [email protected] 9999 9000

110 cust10 [email protected] 1010 10000

111 cust11 [email protected] 1011 11000

111 cust11 [email protected] 1011 11000

110 cust10 [email protected] 1010 10000

109 cust9 [email protected] 9999 9000

108 cust8 [email protected] 8888 8000

107 cust7 [email protected] 7777 7000

106 cust6 [email protected] 6666 6000

105 cust5 [email protected] 5555 5000

104 cust4 [email protected] 4444 4000

103 cust3 [email protected] 3333 3000

102 cust2 [email protected] 2222 2000

101 cust1 [email protected] 1111 1000

Note:I changed the database content before the before it completed printing of first set of values i.e (count->1)

Upvotes: 1

Views: 2152

Answers (2)

Muhammad
Muhammad

Reputation: 7324

The field ResultSet.TYPE_SCROLL_SENSITIVE creates a ResultSet object whose cursor can move both forward and backward relative to the current position and to an absolute position.

The field ResultSet.CONCUR_UPDATABLE creates a ResultSet object that can be updated while CONCUR_READ_ONLY creates a ResultSet that cannot be updated but only it can be read.

The problem that you cannot see any changes that you made in the database table is because you are using read only type ResultSet.

Upvotes: 0

Gerold Broser
Gerold Broser

Reputation: 14772

Maybe the key point lies in ResultSet.TYPE_SCROLL_SENSITIVE's JavaDoc:

"[...] a ResultSet object that is scrollable and generally sensitive to changes to the data that underlies the ResultSet."

Maybe "generally" is rather meant to be usually here, not universally. In the sense of being a CAN, not a MUST. But with high probability that it's implemented. (But maybe that's just because I'm not a native English speaker :-)

What if you insert/delete records? Did you try it with different DBs?

Upvotes: 0

Related Questions