【发布时间】:2010-10-12 18:12:06
【问题描述】:
如何使用 sql server 2005 将 Spring 与 Hibernate 集成并支持 Unicode。 我尝试了很多不同的方法,但我就是无法让它发挥作用。
表中的列是nvarchar,Spring中的字符集是UTF-8。 我可以阅读 Unicode 文本(我自己使用 sql server 管理工具添加的),但写入不起作用,它在数据库中被乱码。
jdbc 网址是
jdbc:sqlserver://localhost:1433;useUnicode=true;characterEncoding=UTF-8;databaseName=test;
在休眠配置文件中使用这些属性
<property name="hibernate.connection.useUnicode">true</property>
<property name="hibernate.connection.charSet">UTF8</property>
我还有一个过滤器可以更改所有页面的编码
response.setContentType("text/html; charset=UTF-8");
request.setCharacterEncoding("UTF8");
chain.doFilter(request, response);
//do it again, since JSPs will set it to the default
response.setContentType("text/html; charset=UTF-8");
request.setCharacterEncoding("UTF8");
有没有好心人成功做到这一点,可以提供帮助?
非常感谢!
【问题讨论】:
-
检查一下,但我不相信 SQL Server 支持 UTF8。我认为你能得到的最接近的近似值是他们的 UCS-2 编码。见this MSDN article。
-
如果我不能使用 UTF-8 ,最好知道如何使用 UCS-2(或 UTF-16..)来做到这一点。除了把charset改成UCS-2还有什么特别的吗?
标签: sql-server hibernate spring unicode