This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes JDBC and the fly likes refresh Row not working Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "refresh Row not working " Watch "refresh Row not working " New topic
Author

refresh Row not working

Rajesh Vijaya
Ranch Hand

Joined: Oct 18, 2005
Posts: 48
Hi Guys ,

I'm trying to check the refreshRow() of resultset Object.I'm retreiving rows from the table account.After Retreiving all the rows i'm halting the program for a while and then updating a row and then refreshing each row and retreiving ...but there is no change ..I'm updating the database manually

here is my code ,

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

Connection con = DriverManager.getConnection("jdbc dbc:mySQLDSN","","");
Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);

ResultSet rs = stmt.executeQuery("Select * FRom account");


while(rs.next()) {

System.out.println(rs.getInt(1)+rs.getString(2)+rs.getString(3)+rs.getDouble(4));

}

System.in.read();// At this point i'm updating the database manually

while(rs.previous()) {

rs.refreshRow();


System.out.println(rs.getInt(1)+rs.getString(2)+rs.getString(3)+rs.getDouble(4));



}


thanks

Rajesh


Rajesh <br /> <br />SCJP1.4 SCWCD1.4 SCBCD 1.3 ,SCDJWS(Preparing..)<br /> <br />There is no free will.It is the phenomenon bound by cause and effect.But there is something behind will which is free---Swami Vivekananda...
Scott Selikoff
Saloon Keeper

Joined: Oct 23, 2005
Posts: 3700
    
    5

Seriously depends on whether or not this is a J2EE application. In J2EE, most people use transactions to write data (container managed transaction) so if you're accessing in multiple different ways, its completely possible the data you are writing is not available to the application at the time it is being written.

Its a common problem to say the least. How are you writing to the database and is there a transaction manager present? I would read the API on this method refreshRow():

http://java.sun.com/j2se/1.4.2/docs/api/java/sql/ResultSet.html

In general, its not a commonly used method since a) if you changed the data, you should all ready have it and not need to query it and b) usually u'd re-quire the database since this is easy to support and maintain.


My Blog: Down Home Country Coding with Scott Selikoff
Sagar Gudala
Greenhorn

Joined: Nov 08, 2005
Posts: 2
Rajesh,

I tried your scenario on Oracle. While you are updating the database manually, you just have to commit the changes then re-fetches the updated records from the database (READ COMMITTED).


Thanks<br />-Sagar
Rajesh Vijaya
Ranch Hand

Joined: Oct 18, 2005
Posts: 48
Its not a J2EE application.I'm just a beginner trying to learn JDBC.
I'm using MYSQl database and using simple queries to update the database.I Guess mysql is autocommit by default.Besides vat do the CONCUR_READ_ONLY and CONCUR_UPDATABLE mean !!!


Rajesh
Scott Selikoff
Saloon Keeper

Joined: Oct 23, 2005
Posts: 3700
    
    5

It all matters on how your write statement is established and maintained (which you didnt list code for). Usually, if elements are in the scope of the same transaction they read the same data irregardless of whether that data has been commited to the database. But this feature is not automatic as I believe you found. If you have two 'distinct' connections reading/writing the same data they will normally function independently (as two separate clients) and only show data to each other after they each commit.

This issue your having isn't a java or jdbc issue, its general database transaction knowledge issue. I'd read up on this topic so that you under what database isolation levels are and so you can then apply them in your program.
[ November 08, 2005: Message edited by: Scott Selikoff ]
Rajesh Vijaya
Ranch Hand

Joined: Oct 18, 2005
Posts: 48
Scoott ,

I'm using Mysql Database.I'm doing the following things

1) With the help of java code i'm retreiving the contents of the table into the result set object and printing them.Now my result object points the After Last.
2)I'm halting the program for a moment so that i can update the row of a database manually.By manually i mean , i'm executing basic SQL commands to update the table in an another command window.
3) Once the update is done.Now bringing the program back to execution by pressing enter key.The program refreshes each row before retreiving the values from the table .I think i shud expect the latest data.but its not so!!

sample output
Before Maual update
1 Rajesh 1000
2 Dilip 1500

now i'm updating the second row balance to 2000

After Maual update
1 Rajesh 1000
2 Dilip 2000

Only when i execute the program again ...i see the changes ....


thanks in advance ...

Rajesh
 
jQuery in Action, 2nd edition
 
subject: refresh Row not working
 
Similar Threads
retreiving all rows from a mysql table to a jsp page
problem retreiving values from ArrayList
visual J++ and retreiving rows from sql server
Problem when Retreiving from DataBase
Error Retreiving a resultset from an oracle stored procedure