aspose file tools*
The moose likes Java in General and the fly likes a bit puzzled Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "a bit puzzled" Watch "a bit puzzled" New topic
Author

a bit puzzled

joew weakers
Ranch Hand

Joined: Aug 21, 2003
Posts: 48
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

Joined: Jul 11, 2001
Posts: 14112
Well, you cannot cast a String to a Color. What does the String look like?


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Adrian Yan
Ranch Hand

Joined: Oct 02, 2000
Posts: 688
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

Joined: Aug 21, 2003
Posts: 48
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

Joined: Oct 02, 2000
Posts: 688
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

Joined: Aug 21, 2003
Posts: 48
Cheers Adrian. will give it a bash
Rodney Woodruff
Ranch Hand

Joined: Dec 04, 2001
Posts: 80
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.


Hope This Helps
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

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.


[Jess in Action][AskingGoodQuestions]
Joel McNary
Bartender

Joined: Aug 20, 2001
Posts: 1817

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 ]

Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
Posts: 1923

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.


http://home.arcor.de/hirnstrom/bewerbung
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: a bit puzzled