为什么Unicode字符在数据库中正确显示,但是作为?当通过Hibernate打印Java? [英] Why do Unicode characters show up properly in database, but as ? when printed in Java via Hibernate?

查看:111
本文介绍了为什么Unicode字符在数据库中正确显示,但是作为?当通过Hibernate打印Java?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个web应用程序,并使用Hibernate 3.5与MySQL进行接口。使用数据库作为我的测试字符串,我可以输入字符串并查看它是否正确地保存到数据库中。但是,当我稍后将值从数据库中拉出并以字符串形式打印到控制台时,我会看到?????? ????????。如果我使用

  new OutputStreamWriter(System.out,UTF-8); 

然后我得到ÉáÇπÇàÉÉÉÉ·Éò·Éú·为什么我看不到原始字符串?



这些是我的hibernate.cfg .xml设置:

 < property name =hibernate.connection.useUnicode> 
true
< / property>
<属性名称=hibernate.connection.characterEncoding>
UTF-8
< / property>
< property name = hibernate.connection.charSet>
UTF-8
< / property>

这是我的数据库连接字符串:

  hibernate.connection.url = jdbc:mysql:// localhost / mydatabase ?autoReconnect = true& useUnicode = true& amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; 

它的控制台没有配置为使用 UTF-8 来显示它们。对于Eclipse,您可以通过 Window> Preferences> General> Workspace> Text File Encoding 配置其编码。它应该被设置为 UTF-8


$ b $ <新的OutputStreamWriter(System.out ,UTF-8);
仅指示 OutputStreamWriter 使用哪种编码将写入的字符转换为字节。它不指示 System.out 控制台使用哪种编码将它们从字节转换为字符转换为显示


I'm writing a webapp, and interfacing with MySQL using Hibernate 3.5. Using "デスクトップ ინგლისური" as my test string, I can input the string and see that it is properly persisted into the database. However, when I later pull the value out of the database and print to the console as a String, I see "?????? ?????????". If I use

new OutputStreamWriter(System.out,"UTF-8");

then I get "デスクトップ ინგლისური"". Why don't I see the original string?

These are my hibernate.cfg.xml settings:

<property name="hibernate.connection.useUnicode">
    true
</property>
<property name="hibernate.connection.characterEncoding">
    UTF-8
</property>
<property name="hibernate.connection.charSet">
    UTF-8
</property>

and this is my database connection string:

hibernate.connection.url = jdbc:mysql://localhost/mydatabase?autoReconnect=true&amp;useUnicode=true&amp;characterEncoding=UTF-8

解决方案

It's the console which is not configured to use UTF-8 to display them. In case of Eclipse, you can configure its encoding by Window > Preferences > General > Workspace > Text File Encoding. It should be set to UTF-8.

The new OutputStreamWriter(System.out,"UTF-8"); only instructs the OutputStreamWriter which encoding to use to convert the written chars to bytes. It doesn't instruct the System.out console which encoding to use to convert them back from bytes to chars to display them.

这篇关于为什么Unicode字符在数据库中正确显示,但是作为?当通过Hibernate打印Java?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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