This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes JDBC and the fly likes JTable and ResultSet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Databases » JDBC
Bookmark "JTable and ResultSet" Watch "JTable and ResultSet" New topic
Author

JTable and ResultSet

Gulsum Ramazanoglu
Ranch Hand

Joined: Jun 27, 2008
Posts: 51
Hi people,
Is there a better way of passing data from a data source to JTable, out of using array? Cant I use ResultSet directly, instead of an array?
Also cant we make all of the changes on the cells of JTable object we display, and reflect the changes directly from the cells to database?
The existence of an array looks cumbersome... is there any other way?
Thank you..
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39084
    
  23
I don't think this is really a beginner's question. A bit of JDBC and a bit of Swing. Difficult to be sure, but you are talking about updating databases, so maybe the JDBC forum would be best for it, and I shall move you . . .
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

I don't see any reason why you couldn't write a TableModel that encapsulated a ResultSet. In fact I would bet somebody has already done that and posted it somewhere on the Internet. The Google keywords I would use for that are "Swing jtable resultset".

As you see I don't think this is a JDBC question. But from the JDBC point of view I don't necessarily think that keeping a ResultSet open and updating it qualifies as "better".

But I won't bother to move this (again) to the Swing forum because I don't think there's much to say beyond "yes".
Ted Smyth
Ranch Hand

Joined: May 28, 2008
Posts: 73
Just make the ResultSet the table's model then. Extend AbstractTableModel and implement getColumnName(), getColumnCount(), getRowCount(), etc...

Override getValueAt(){

}

And include a method to set the model to a new ResultSet and fire off a fireTableDataChanged();

Good to go!

EDIT: I typically like to encapsulate my DB results into a custom Bean of some sort, rather than using the ResultSet (since I typically modify these Beans elsewhere in my app, not just display via a Table), but that's just my preference.
[ July 07, 2008: Message edited by: Ted Smyth ]

Edward Smith
 
jQuery in Action, 2nd edition
 
subject: JTable and ResultSet