开发者

Why do Unicode characters show up properly in database, but as ? when printed in Java via Hibernate?

开发者 https://www.devze.com 2022-12-25 23:16 出处:网络
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

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;ch开发者_如何学JAVAaracterEncoding=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.


Maybe its an issue with your JDBC driver? Try using a different one, see if the problem persists.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号