Problems reading/writing UTF-8 data in MySQL from Java using JDBC connector 5.1

后端 未结 3 1019
[愿得一人]
[愿得一人] 2020-11-30 09:16

I have a scenario with two MySQL databases (in UTF-8), a Java code (a Timer Service) that synchronize both databases (reading form first of them and writing/updating to seco

相关标签:
3条回答
  • 2020-11-30 09:27

    At some point in the chain, UTF-8–encoded bytes are being decoded with Latin1. From the list of your settings, it appears this is happening at "character_set_server". Without knowing how these values were obtained, it is hard to interpret them.

    Check the value of the system property "file.encoding". If that is not "UTF-8", then you need to explicitly specify "UTF-8" as the character encoding whenever you decode bytes to characters. For example, when you call a String constructor with a byte[], or use an InputStreamReader.

    It is best to explicitly specify character encodings, rather than rely on the default platform encoding.

    0 讨论(0)
  • 2020-11-30 09:35

    You can set the file.encoding property of your JVM to UTF-8 so all locale/encoding sensitive API will consider decoded Strings as UTF8.

    For example, you can set it in your command line that launches your Java app:

    java -Dfile.encoding=UTF-8 ....
    

    You can also refer to this SO question for a complete explanation of Tomcat setup.

    0 讨论(0)
  • 2020-11-30 09:50

    A little late but this will help you:

    DriverManager.getConnection(
               "jdbc:mysql://" + host + "/" + dbName 
               + "?useUnicode=true&characterEncoding=UTF-8", user, pass);
    
    0 讨论(0)
提交回复
热议问题