• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Updating a result set

 
Ranch Hand
Posts: 455
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi. I am using a PreparedStatement to get a ResultSet. I have looked at a couple of examples on how to make my ResultSet updatable, but they are all using Statement. Does anyone know how to make a PreparedStatement ResultSet updatable?
Thanks
[ October 07, 2002: Message edited by: Jennifer Sohl ]
[ October 07, 2002: Message edited by: Jennifer Sohl ]
 
Jennifer Sohl
Ranch Hand
Posts: 455
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK. I found some code to make PreparedStatement updatable, however, it doesn't seem to be working. Could someone look at this code and see what I'm doing wrong?

This compiles and runs without error, but when I try putting my result set into a table, I am not seeing any changes.
(The variable 'dflt' is a sql date of 1900-01-01. Basically if this is the date in the result set, I want to change it to display blanks)
Thanks again.
[ October 07, 2002: Message edited by: Jennifer Sohl ]
 
Ranch Hand
Posts: 1879
MySQL Database Suse
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
make sure that your dates are equal right down to the millisecond value:
public boolean equals(Object obj) - Compares two dates for equality. The result is true if and only if the argument is not null and is a Date object that represents the same point in time, to the millisecond, as this object. Thus, two Date objects are equal if and only if the getTime method returns the same long value for both.
it seems to me like you want to compare only the yyyymmdd portions of the dates.
Jamie
 
Jennifer Sohl
Ranch Hand
Posts: 455
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the reply. I have tested to see if the dates are actually equal. I believe they are, because otherwise my System.out wouldn't run that is included in that "if" statement, and it does.
I also checked the concurrency and it brings it back as updatable.
I am assuming that I'm using the "updateXXX" method properly? I can update a row in the result set, and then display it in a table and it should give me the updated data correct?
Or do I have to do something else?
Thanks again!
[ October 08, 2002: Message edited by: Jennifer Sohl ]
 
Jennifer Sohl
Ranch Hand
Posts: 455
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm baack! OK, I played around a little bit and now I'm really confused. I modified my code a bit. Here's what I did:

I changed the while loop to update the database with the changes, and guess what?? The JTable displayed the changes?? My problem is I don't want to update the database, just the ResultSet. As you can see, I also did a System.out.println to get the value in the ResultSet in that column right after the updateRow() had taken place, and the value that printed to std out was the old value. However, the new value displayed in the table??? What is going on here?
Can someone help?

Thanks!
 
Jamie Robertson
Ranch Hand
Posts: 1879
MySQL Database Suse
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if you don't want the database to be updated, why are you keeping the resultset around when they are just tying up resources? It seems that you could query the database, save the resultset as an ArrayList, and display the arraylist in the JTable. Allow the user/program to make necessary changes to the arraylist whilst refreshing the GUI to reflect these changes. When all is said and done, and you want the actual update to take place, fire and update SQL statement ( or series of statements ) on the rows that you want to update. Maybe I'm just not fond of scrollable resultsets because they hog resources ( db and memory ) and can be easily abused, but I think you would be better served by separating the database stuff from the GUI.
If your having problems with the behaviour of the JTable regarding refreshing the updated values, you may get more timely answers in the Swing forum.
Jamie
 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Take a look at the documentation of the Connection's interface method :
PreparedStatement prepareStatement(String sql,
int resultSetType,
int resultSetConcurrency)
using the method above you can create a updateable prepared statement.
Try it and be happy.
 
reply
    Bookmark Topic Watch Topic
  • New Topic