Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Manually Alter ResultSet + ResultSetTableModel

 
Marcus Hathaway
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

Is it possible to manually change the data in a resultset?

With the code i'm working with, a table eventually gets populated with a ResultSetTableModel:

model = new ResultSetTableModel(resultSet);

This works fine, displaying the columns and data from the resultset.

However, one of the values of the resultset is an int value of 1 or 0. Instead of displaying 1 or 0 on the JTable, i would like to display "Yes" or "No" instead. Is it possible to manipulate the result set in some way to do this before i make the call:

model = new ResultSetTableModel(resultSet);

I can think of alternative ways to populate the JTable where this would be easy to achieve. However, it would make life lots easier if i could simply alter the resultset and leave the rest of the code untouched.

Cheers for any advice!
 
Shailesh Chandra
Ranch Hand
Posts: 1082
Java Oracle Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would recommend you do this task ouside your resultset, possibly in your ResultSetTableModel.

You can also modify your sql query also to get desired result.
In Oracle I would use Decode()
Also you can use SQL CASE Statements to get desired result



Shailesh
 
Marcus Hathaway
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Shailesh,

Altering the SQL statement worked a treat.

 
Paul Clapham
Sheriff
Posts: 21581
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's nice that it works but it isn't the right approach.

Since you are asking about how things are to be displayed, you should really have the view logic deal with that, rather than changing the data in the model. (JTable is the view and TableModel is the model, and they are separate for specific architectural reasons.) So my suggestion would be to have your table cell renderer map 1 to Yes and 0 to No.
 
Shailesh Chandra
Ranch Hand
Posts: 1082
Java Oracle Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Paul Clapham:
So my suggestion would be to have your table cell renderer map 1 to Yes and 0 to No.


I agree with you !!! It should have been handled with presentation layer, But altering query is also one adopted approach

Shailesh
[ May 25, 2006: Message edited by: Shailesh Chandra ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic