permaculture playing cards
The moose likes JDBC and Relational Databases and the fly likes UTF-decoding issue with java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "UTF-decoding issue with java" Watch "UTF-decoding issue with java" New topic

UTF-decoding issue with java

Arun Chaitanya

Joined: Dec 30, 2008
Posts: 1
I fetch data from DB which has UTF-8 encoding for a column, When i read it in the form a string the value is in the form of \u00f1, while i expect it to be decoded. Can you help me out on how i am supposed to read this character set such that when i read i read the decoded string instead of unicode characters
Mark Vedder
Ranch Hand

Joined: Dec 17, 2003
Posts: 624

Welcome to JavaRanch.

When you are creating your database connection, are you explicitly setting it to use UTF-8 for encoding? If not, it is likely not defaulting to UTF-8.

Typically you will need to set this on your DataSource. It will vary from database implementation to database implementation. With MySQL, for example, there is a setEncoding(String) method. Many databases (or DataSource implementations) allow you to set the encoding as part of the URL string used when creating the DataSource. For example, in MySQL, you can do this:

The syntax used will vary. Check your Database documentation for more information.

After rereading your post, I see you stated "from DB which has UTF-8 encoding for a column". You mentioned a column rather than the DB. The above applies only if the database itself is encoded using UTF-8. And it won't solve the issue if you are storing the actual string "\u00f1" in the column for the reasons Ulf mentions below.
[ December 30, 2008: Message edited by: Mark Vedder ]
Ulf Dittmer

Joined: Mar 22, 2005
Posts: 42965
Welcome to JavaRanch.

Do you mean that the data that's stored in the DB literally includes something like "\u00f1"? That would be a Java-specific notation of Unicode characters that really should not be stored like this in a DB. I'd advise to store proper Unicode characters instead (set the DB encoding accordingly).
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46334
Sounds like a database-specific problem; it would sit better in our JDBC forum. Moving.
I agree. Here's the link:
subject: UTF-decoding issue with java
jQuery in Action, 3rd edition