• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

a bit puzzled

 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, you cannot cast a String to a Color. What does the String look like?
 
Ranch Hand
Posts: 688
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Cheers Adrian. will give it a bash
 
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 1844
Eclipse IDE Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 1923
Scala Postgres Database Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
reply
    Bookmark Topic Watch Topic
  • New Topic