问题与utf8在java [英] problem with utf8 in java

查看:138
本文介绍了问题与utf8在java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有下表:

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

我插入的数据如下:

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

但是当我检索数据时,它们类似于 / code>。

But when I retrieve data they are resemble to ?????.

什么是问题,我该如何解决?

What is problem and how can I solve that?

推荐答案

根据 MySQL JDBC驱动程序文档 a>您还需要在JDBC连接URL中设置字符编码。这里有一个例子:

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

否则,MySQL JDBC驱动程序将在通过网络发送之前使用平台默认编码将字符转换为字节,这在你的情况下显然不是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.

此外,当检索数据时,您需要确保显示/写入字符的控制台/文件也支持/使用UTF-8。否则他们也会成为问号。

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.

  • Unicode - How to get the characters right?

顺便说一下,您不需要 SET NAMES 在这里查询。

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

这篇关于问题与utf8在java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆