• Post Reply Bookmark Topic Watch Topic
  • New Topic

problem with tablemodel  RSS feed

 
randy butler
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all

I'm having an issue with my jTable allowing the end user to update indivdual cells in the table, then having those update the database. I think my issue lies in the tablemodel but have yet been able to wrap my head around what it is wrong with it. Below is my table model. thanks for any insite on this issue.

 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not real clear on what exactly your question or problem is. You may wish to spend a bit of time describing exactly what happens that shouldn't happen and what precisely you desire to happen that isn't. Details can be important here. Good luck.
 
randy butler
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry for the confusion in my post. I was in a hurry when I made the post, so let me clear it up. I want the user to be able edit data in the cells of the jtable and that edit to update the database.

Thanks
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
randy butler wrote:Sorry for the confusion in my post. I was in a hurry when I made the post, so let me clear it up. I want the user to be able edit data in the cells of the jtable and that edit to update the database.

I may be asking the obvious, but are you adding a TableModelListener to your model? My guess would be that you would create your prepared statement and update the database from the listener's tableChanged method. Again, this may already be obvious to you, or I could be wrong.
 
randy butler
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pete thanks for the quick response. I am not using TableModelListener and not sure how I would add it. Can you show me an example.

Thanks
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
randy butler wrote:Pete thanks for the quick response. I am not using TableModelListener and not sure how I would add it. Can you show me an example.

It only has one method, so it's not that hard to implement, but I am not a pro on this, so best I can do is to refer you to the tutorial on TableModelListeners here: How to Write a Table Model Listener

Also I searched the Sun forum and found this interesting link: how do I update database from my JTable?

Hope it helps.
 
Mikko Kohtamäki
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Something quick


Or you could override these methods and work something out:
 
randy butler
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Mikko for the example. I'm confused trying to read through it to understand what I need to do to add it to my own code

I also get the following error when try to edit a cell in the table
Operation not allowed after ResultSet closed
 
Brian Cole
Author
Ranch Hand
Posts: 949
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
pete stein wrote:I may be asking the obvious, but are you adding a TableModelListener to your model? My guess would be that you would create your prepared statement and update the database from the listener's tableChanged method.


I'm confused by this response. Yes, it's possible to have a TableModelListener that contains some PreparedStatement code, but wouldn't it be easier to just put that same PreparedStatement code in the model's setValueAt() method?

Of course, the setValueAt() method is already trying to use JDBC calls to update the database--it's just trying to use a scrollable ResultSet and updateRow, no a PreparedStatement.

I'm still waiting for Mr. Butler to respond to Mr. Stein's request for details on what the exact problem is. How does the actual behavior differ from the expected behavior? Ok, the user is not "able edit data in the cells" but what exactly happens when the user tries to do so? Is an SQLException thrown or what?
 
randy butler
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Brian,

details on what the exact problem are when the user enters data in a cell and hits enter or moves to another cell we get the following error
ResultSet closed and the error points to my setValueAt() method.

the expected behavior is that when the user enters data in a cell or any cell it updates the database and the jtable.

thanks for your time
 
Mikko Kohtamäki
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The question is, how you create your Statement?

Using connection.createStatement() the ResultSet isn't updatable and cursor can move forward only.

Use connection.createStatement(int resultSetType, int resultSetConcurrency).

ResultSet.CONCUR_UPDATABLE defines that ResultSet is updatable and more you can read from the API doc...
http://java.sun.com/j2se/1.4.2/docs/api/java/sql/ResultSet.html

And for 'ResultSet closed error', is the Statement closed which created it (it closes ResultSet too)? Or your ResultSet just isn't updatable?
 
Brian Cole
Author
Ranch Hand
Posts: 949
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
randy butler wrote:when the user enters data in a cell and hits enter or moves to another cell we get the following error
ResultSet closed and the error points to my setValueAt() method.

the expected behavior is that when the user enters data in a cell or any cell it updates the database and the jtable.


Well, the ResultSet may actually be closed. If so, and if you want to keep the scrollable-ResultSet and updateRow() approach, you need to stop that from happening. You also need to make sure your ResultSet is actually scrollable (and not just TYPE_FORWARD_ONLY scrollable).

What does calling aResultSet.getType() return? (Compare with values listed here.)
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!