Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

a bit puzzled

 
joew weakers
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Any ideas on how to fix this. The problem is that I need to execute the SQL statement at runtime and the setFillColor method needs a color as an argument. The result of the query is a specific color, e.g. blue. I am getting the following error when I compile the code below:
cannot cast java.lang.String to java.awt.Color

Connection cont = new Connection()
Statement stmt = cont.conn.createStatement();
String query = "Select layer_color, layer_size from user_layers where user_id in \n" + "(select user_id from map_users where user_name = 'Frank Bunce') \n" + "and layer_id in (select layer_id from map_layers where layer_name = 'Cities')";
OracleResultSet ors = (OracleResultSet) stmt.executeQuery(query);

while(ors.next())
{
String dbresult = ors.getString(1);
Color color = (Color) dbresult; //error results from this line
ompoint.setFillColor(color);
}
graphics.add(ompoint);
Cheers Joe
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, you cannot cast a String to a Color. What does the String look like?
 
Adrian Yan
Ranch Hand
Posts: 688
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yea, dude. You have a String of dbresult, you can't cast a String object into a Color object no matter what. Also, your code is abit dangerous, since Color class support only a few default colors, lokks at it's constants. You better off store the RGB value of the color in your database, so the Color class can convert it better.
 
joew weakers
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello there. Thanks for the responses but still I am not sure about what to do. The result of the SQL query is going to be a specific colour. Therefore the String dbresult is going to hold a value like red, blue, green etc. Now I want to pass this string value into the setFillColor method which takes a color as an argument. Seeming as dbresult will hold a valid color in the form of a string, is there any way to pass this value into setFillColor. Cheers Joe
 
Adrian Yan
Ranch Hand
Posts: 688
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, but a rather painful one. You need to have a switch statement, and test each case, so, case "RED": return Color.RED, etc. then you can contruct a Color object new Color(Color.RED) and pass it to setFilledColor. Hope that helps.
 
joew weakers
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cheers Adrian. will give it a bash
 
Rodney Woodruff
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Adrian has the right solution -- store the rgb values in the database. Then your code would be
while(ors.next())
{
Int dbresult = ors.getInteger(1);
Color color = new Color(dbresult); //error results from this line
ompoint.setFillColor(color);
}
This way you can have many different color combinations -- roughly 8 bit color.
Nonetheless, if changing your database scheme is not possible right now, you could do the following:
1. Create a class that extends or maintains a hashtable (composition).
2. You then implement two methods addColor(String name, int rgb) and getColor(String name).
3. Initialize the class with all the colors that you expect from your database calls. Some sample code is below:

Then your code would be:

I don't recommend this approach but it should get your around the problem that you are currently facing.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Adrian's answer is correct in theory, but of course Java doesn't let you use Strings as cases in a switch statement. If the number of possible colors is small, then just use a chain of if-then statements:

If the number is large, then this would be a lot of messy, slow code, so it would be smarter to put all the colors into a HashMap with the Strings as keys, the colors as values; then you can do fast lookup.
 
Joel McNary
Bartender
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, if the values in the database are controlled and are exactly the names of the colors ("RED", etc.), then you can use Reflection to populate that HashMap. Take a look at the sample code I wrote below (This returns ints instead of java.awt.Colors and is for SQL instead of AWT and has a custom java.util.Map implementation, but it should give you the general idea...)
(map is a class member variable, a Translator is a class that holds a 1:1 relationship between a key and a value; that is, there is a KeySet and an Value set; you can obtain the Key form the Value and the Value from the Key.)

[ October 09, 2003: Message edited by: Joel McNary ]
 
Stefan Wagner
Ranch Hand
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by joew weakers:

String query = "Select layer_color, layer_size from user_layers where user_id in \n" + "(select user_id from map_users where user_name = 'Frank Bunce') \n" + "and layer_id in (select layer_id from map_layers where layer_name = 'Cities')";

Where did you learn sql?
String query =
"Select
layer_color,
layer_size
from user_layers ul,
map_users mu,
map_layers ml
where ul.user_id = mu.user_id
and user_name = 'Frank Bunce'
and ml.layer_id = ul.layer.id
and layer_name = 'Cities'";
- ok, that wasn't the Question.
I would recommend storing the color as int instead of name, if possible, or use the example with the hashmap.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic