Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JTable and ResultSet

 
Gulsum Ramazanoglu
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 20959
31
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic