简体   繁体   English

java中的utf8问题

[英]problem with utf8 in java

I have the following table: 我有下表:

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

I am inserting data as follows: 我按如下方式插入数据:

resultSet.executeQuery("set namee utf8");
preparedStatement = connection.prepareStatement("insert into test(fname) values(?)");
preparedStatement.setstring(1,"alis");
preparedStatement.executeUpdate();

But when I retrieve data they are resemble to ????? 但是当我检索数据时,它们就像是????? .

What is problem and how can I solve that? 什么是问题,我该如何解决?

As per the MySQL JDBC driver documentation you need to set the character encoding in the JDBC connection URL as well. 根据MySQL JDBC驱动程序文档,您还需要在JDBC连接URL中设置字符编码。 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. 否则,MySQL JDBC驱动程序将使用平台默认编码在通过网络发送之前将字符转换为字节,在您的情况下显然不是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. 此外,在检索数据时,您需要确保显示/写入字符的控制台/文件也支持/使用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: 也可以看看:


By the way, you don't need the SET NAMES query here. 顺便说一下,这里不需要SET NAMES查询。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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