java中utf8的问题

发布于 2024-11-02 09:41:51 字数 455 浏览 0 评论 0原文

我有下表:

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();

但是当我检索数据时,它们类似于 ??????

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

凌乱心跳 2024-11-09 09:41:51

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

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

否则,MySQL JDBC 驱动程序将使用平台默认编码在通过网络发送之前将字符转换为字节,在您的情况下显然不是 UTF-8。所有未覆盖的字符将被替换为问号。

此外,在检索数据时,您需要确保显示/写入字符的控制台/文件也支持/使用 UTF-8。否则它们也会成为问号。如何解决这个问题取决于您显示/写入这些字符的方式/位置。

另请参阅:


顺便说一句,您不需要此处的SET NAMES 查询。

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:


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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文