problem with utf8 in java

前端 未结 1 1060
后悔当初
后悔当初 2021-01-05 13:57

I have the following table:

create table test
(
  fname char(20) character set utf8 collate utf8_turkish_ci,
  id int primary key
);

I am i

相关标签:
1条回答
  • 2021-01-05 14:56

    As per the MySQL JDBC driver documentation you need to set the character encoding in the JDBC connection URL as well. Here's an example:

    jdbc:mysql://localhost:3306/db_name?useUnicode=yes&characterEncoding=UTF-8
    

    Otherwise the MySQL JDBC driver will use the platform default encoding to convert the characters to bytes before sending over network, which is in your case apparently not UTF-8. All uncovered characters will then be replaced by question marks.

    Also, when retrieving the data, you need to ensure that the console/file where you're displaying/writing the characters to also supports/uses UTF-8. Otherwise they will become question marks as well. How to fix that depends on how/where you're displaying/writing those characters to.

    See also:

    • Unicode - How to get the characters right?

    By the way, you don't need the SET NAMES query here.

    0 讨论(0)
提交回复
热议问题