• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Can't write UTF8 chars

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

I don't succeed to write to my MySql db with UTF-8.
I can read Characters in UTF-8 but I can't write.
If I try to save aaaéaaa, I get in my db browser (Navicat) aaa?aaa.
With my DB browser I can read and write aaaéaaa freely.


I tried anything I can think of:
• Start mysql server with --character-set-server=utf8
• Define a new table with only UTF8 columns
• Use the JVM flag -Dfile.encoding=utf8
• Compile with: javac -encoding UTF-8
• connection url: ?useUnicode=yes&characterEncoding=utf8
• Convert the String object itself: new String(aString.getBytes("utf8"),"utf8");
• I even tried to translate the whole query to UTF-8
• statement.executeUpdate("set names utf8");
• statement.executeUpdate("set character_set_results=utf8");

What did I miss???

I'm using:
OS: windows XP
Java: Sun 1.6.0_15
driver: mysql-connector-java-5.0.4.jar
Mysql server: 5.1.37


Here's the source code:


Any help will be appreciated,
Eran

 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


String aStr = "aaaéaaa" + System.currentTimeMillis();
byte[] strBytes = aStr.getBytes("utf8");
String aStrUtf8 = new String(strBytes,"utf8");


This part is odd. For starters, it's better to use the official encoding names: "UTF-8" instead of "utf8".

But Java uses UTF-16 for String objects, so aStrUtf8 is not actually in UTF-8, but UTF-16. The second and third line perform the exact opposites of one another - aStr should be identical to aStrUtf8.
 
Rancher
Posts: 377
Android Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey,

I had the exact same problem and it was down to the default character encoding on the JVM (it was set to American ASCII so all unknown characters got a ?).

Anyway you can check the encoding with a simple bit of code, this link shows how to do it:
http://www.rgagnon.com/javadetails/java-0505.html

Good luck!

Sean
 
Eran Kaplan
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the answers.

Eventually, I changed my MySql server from the .zip without installer version to the .msi Windows Essentials version and it worked.

Ulf Dittmer, you are right. I got confused by some code sample in the web.

Thanks again.


 
And then we all jump out and yell "surprise! we got you this tiny ad!"
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic