java中utf8的问题
我有下表:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据 MySQL JDBC 驱动程序文档您还需要在 JDBC 连接 URL 中设置字符编码。这是一个示例:
否则,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:
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.