gle*_*tov 4 java mysql unicode encoding
我正在使用MySQL版本5.1.45,Tomcat 5.5.28和Hibernate 3编写web应用程序
当我试图保存包含非拉丁字符的字符串(例如Упячка)时,会发生错误:
1589 [main] WARN org.hibernate.util.JDBCExceptionReporter - SQL Error: 1366, SQLState: HY000
1589 [main] ERROR org.hibernate.util.JDBCExceptionReporter - Incorrect string value: '\xD0\xA3\xD0\xBF\xD1\x8F...' for column 'name' at row 1
Run Code Online (Sandbox Code Playgroud)
休眠连接设置
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost/E2012?characterEncoding=UTF8&useUnicode=true</property>
<property name="connection.username">***</property>
<property name="connection.password">***</property>
<property name="hibernate.connection.charSet">UTF8</property>
Run Code Online (Sandbox Code Playgroud)
MySQL配置My.cnf
[client]
default-character-set=utf8
[mysqld]
default-character-set=utf8
Run Code Online (Sandbox Code Playgroud)
即使查询集名称utf-8也无法解决问题
感谢帮助!
在UTF-8中,??????
实际应该表示为\x423\x43F\x44F\x447\x43A\x430
.在\xD0\xA3\xD0\xBF\xD1\x8F...
暗示他们被错误地使用了ISO-8859-1编码.
这是一个测试片段,证明了这一点:
String s = new String("??????".getBytes("UTF-8"), "ISO-8859-1"); // First decode with UTF-8, then (incorrectly) encode with ISO-8859-1.
for (char c : s.toCharArray()) {
System.out.printf("\\x%X", (int) c);
}
Run Code Online (Sandbox Code Playgroud)
哪个打印
\xD0\xA3\xD0\xBF\xD1\x8F\xD1\x87\xD0\xBA\xD0\xB0
Run Code Online (Sandbox Code Playgroud)
所以你的问题需要先解决一步.由于您正在讨论Java Web应用程序,并且此字符串可能是由用户输入引起的,您确定已经注意了HTTP请求和响应编码吗?首先,在JSP中,您需要将以下内容添加到JSP的顶部:
<%@ page pageEncoding="UTF-8" %>
Run Code Online (Sandbox Code Playgroud)
这不仅以UTF-8呈现页面,而且还隐式设置HTTP Content-Type
响应头,指示客户端使用UTF-8呈现页面,以便客户端知道它应该显示任何内容并使用相同的编码.
现在,HTTP请求部分,对于GET请求,您需要配置相关的servletcontainer.在Tomcat中,例如,这是设定的事项URIEncoding
的的属性HTTP连接器中/conf/server.xml
相应.对于POST请求,客户端(webbrowser)应该已经足够聪明地使用JSP中指定的响应编码.如果没有,那么你就需要在带来Filter
哪些检查和设置请求编码.
有关更多背景信息,您可能会发现本文很有用.
除此之外,MySQL还有另一个Unicode字符问题.它仅支持最多3个字节的 UTF-8字符,而不支持4个字节.换句话说,只支持65535个字符的BMP范围,不支持.例如PostgreSQL完全支持它.这可能不会伤害您的Web应用程序,但这当然要记住.
归档时间: |
|
查看次数: |
3536 次 |
最近记录: |