Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
The moose likes Java in General and the fly likes Unicode Charaters from a database Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Unicode Charaters from a database" Watch "Unicode Charaters from a database" New topic

Unicode Charaters from a database

John Harris

Joined: Oct 20, 2005
Posts: 7
Hi, I'm trying to retrieve the unicode values of currency symbols from a database (we had lots of problems with locales so this is the way it's being done).
In the database, the unicode representations eg \u20ac have been defined as Varchar2 but when they get into the Java layer, they appear as "\\u20ac". I have tried returning just the 20ac part & appending "\u" to it but JBuilder complains about illegal unicode escape characters.
Anyone know how I can arrive at the equivalent of String x = "\u20ac" ??
Eric McIntyre

Joined: Mar 03, 2005
Posts: 26
I think you're on the right track. I came up with this, but it's hard to verify in a DOS prompt:

Whaddaya think?
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
The reason JBuilder complains is that "\u" indicates the start of an escape sequence. It expects the unicode value after the 'u', but doesn't see it. There is a way to fix this so it will compile, but I doubt it will do anygood. Instead, I have a few questions:

1) Does your database support Unicode characters in a column with type VARCHAR?

2) If so, why don't you just store the single character rather than a string representation of the Unicode character? At the moment, as you can see, the database is storing the literal characters, not the unicode value.

3) If not, you could store it as an integral type and cast it to a char when you read it into your Java program. To me either of these solutions seems like less of a hassle than you current solution.

[ October 24, 2005: Message edited by: Layne Lund ]

Java API Documentation
The Java Tutorial
Akshay Kiran
Ranch Hand

Joined: Aug 18, 2005
Posts: 220
the code snip by eric seems the only likely way, unicodes are decoded at compile time, there's no way you can have a string "\u0012" with its unicode meaning at runtime- it wil just be a srting of 6 chars.

"It's not enough that we do our best; sometimes we have to do<br />what's required."<br /> <br />-- Sir Winston Churchill
I agree. Here's the link:
subject: Unicode Charaters from a database
It's not a secret anymore!