简体   繁体   中英

MySQL, Java and UTF8

I have a database with MEDIUMBLOB fields in tables with ENGINE=InnoDB DEFAULT CHARSET=utf8 on each table.

I connect to the database with useUnicode=true&characterEncoding=UTF8&character_set_client=UTF8&character_set_database=UTF8&character_set_results=UTF8&character_set_server=UTF8&character_set_system=UTF8 on the URL.

(I have tried with just useUnicode=true&characterEncoding=UTF8 with the same results.)

When I look at values in the database using mysql from the command-line, I can see non-ASCII characters just fine.

But when I read in strings from the database using executeQuery() getString() I get gibberish. How can I read in the values correctly?

I am using log4j to show the strings I am writing to the database, and those I read back, and those I write show fine.

That's a BLOB, not a string. So you want getBytes and decode it manually.

 new String( bytes, "UTF-8"); //Subsitute "UTF-8" with whatever encoding the blob is in

If you want more transparent support, defined a mediumtext column with UTF-8. Then all that configuration will become effective and you get proper strings automatically with getString.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM