File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JDBC and Relational Databases 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 and Relational Databases
Bookmark "JTable and ResultSet" Watch "JTable and ResultSet" New topic

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

Joined: Oct 13, 2005
Posts: 46367
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

Joined: Oct 14, 2005
Posts: 19973

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
I agree. Here's the link:
subject: JTable and ResultSet
It's not a secret anymore!