wood burning stoves 2.0*
The moose likes Java in General and the fly likes Java reading from utf-8 MySql problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Java reading from utf-8 MySql problem" Watch "Java reading from utf-8 MySql problem" New topic

Java reading from utf-8 MySql problem

Ernest Gungl

Joined: Feb 21, 2009
Posts: 5
Hello friends!

Well, I have a problem with reading from MySql database (db). I use MySQL version: 5.5.20, mysql-connector-java-5.1.14-bin.jar and jre6. I start MySql server with: mysqld --character-set-server=utf8 --collation-server=utf8_unicode_ci (utf-8 representation in MySql).

Eveything, I write from Java application into MySql database works O.K. I can see our special national characters in database excatly as I wrote (or as I would write) them directly in the phpmyadmin (databse editor written in php).

The only problem is that I can't read special national characters from database. I see some graphical symbols instead of my desired national characters. Java uses UTF-16 character representation, but MySql in my case uses utf-8. So, I can belive, that "mysql-connector-java-5.1.14-bin.jar" correctly converts from utf-16 to utf-8 automaticly (so here I talk about writing to database). I DON'T KNOW, WHY THIS CONVERSION (utf-8 in MySql -> utf-16 in java) DOESN'T WORK IN THE OPPOSITE DIRECTION (JAVA READING FROM DATABASE)? Well, reading from Java it doesn't work also when I write my special national characters directly in databse (so I can see my national characters in datbase correctly) through MySql database editor phpmyadmin.

Any suggestion?

best regards,

[Edit: removed excessive use of bold and uppercase]
Martin Vajsar

Joined: Aug 22, 2010
Posts: 3606

How do you display the data after reading them from the database? There certainly is a possibility they are correctly read, but your font cannot properly display them. Also Windows consoles are notoriously known to be unable to display various encoding correctly.

I'd suggest to write the data obtained from the database into a file (making sure the encoding of the output file is properly set) and inspecting the contents of the file, to verify that the data are actually being read wrongly from the database.
Ernest Gungl

Joined: Feb 21, 2009
Posts: 5

Hello Mr. Martin

After reading from database I display message as follows:

I will try with file as you've suggested.

Thank you for reply,
Ernest Gungl

Joined: Feb 21, 2009
Posts: 5
Hello again!

I've tried your idea about writing to file. It works O.K.

Thank you and best regards,
I agree. Here's the link: http://aspose.com/file-tools
subject: Java reading from utf-8 MySql problem
Similar Threads
mysql-connector-java-5.0.4-bin.jar for mysql server 5.1 version
MySQL Connector/J
Session onject throwing exception when trying to save a object or begin a transaction
Fixed: Incorrect DB encoding - posts with ? instead of national characters in UTF-8
Ñ and Tildes problem