hibernate 的中文乱码解决方法

hibernate 的中文乱码解决办法

我用 hibernate Synchronizer 时,在读出和写入数据的时候出现乱码。

解决办法有两种:

第一种办法:分两步:

1、解决保存到数据库时出现的乱码问题。在 hibernate.cfg.xml 中添加两行:

<property name="connection.useUnicode">true</property>
<property name="connection.characterEncoding">gbk</property>

这样,当数据保存时就不会出现乱码。

2、解决从数据库读出数据时的乱码问题。在读出数据后把数据转换为 gbk 编码。

如: new String(getName().getBytes("ISO8859_1"),"gbk"),这时生成的 String 就是可以正常显示的中文了。

第二种办法:(来自:http://www.cnblogs.com/josson/archive/2005/03/16/119619.html)

1, mysql安装时设置字符集utf-8, jdbc驱动3.0.15以上. 
2. hibernate配置文件中,加上属性 
<property name="connection.useUnicode">true</property> 
<property name="connection.characterEncoding">UTF-8</property> 

3. web.xml设置Filter 
================================================= 
hibernate 的中文乱码解决方法<filter> 
hibernate 的中文乱码解决方法  
<filter-name> 
hibernate 的中文乱码解决方法   Set Web Application Character Encoding 
hibernate 的中文乱码解决方法  
</filter-name> 
hibernate 的中文乱码解决方法  
<filter-class>cn.com.commnet.util.SetEncodeFilter</filter-class> 
hibernate 的中文乱码解决方法  
<init-param> 
hibernate 的中文乱码解决方法   
<param-name>defaultencoding</param-name> 
hibernate 的中文乱码解决方法   
<param-value>UTF-8</param-value> 
hibernate 的中文乱码解决方法  
</init-param> 
hibernate 的中文乱码解决方法 
</filter> 
hibernate 的中文乱码解决方法 
<filter-mapping> 
hibernate 的中文乱码解决方法  
<filter-name> 
hibernate 的中文乱码解决方法   Set Web Application Character Encoding 
hibernate 的中文乱码解决方法  
</filter-name> 
hibernate 的中文乱码解决方法  
<url-pattern>/*</url-pattern> 
hibernate 的中文乱码解决方法 
</filter-mapping>

============================================= 
SetEncodeFilter.java 
hibernate 的中文乱码解决方法public class SetEncodeFilter implements Filter 
hibernate 的中文乱码解决方法 
hibernate 的中文乱码解决方法    
protected FilterConfig filterConfig = null
hibernate 的中文乱码解决方法 
hibernate 的中文乱码解决方法    
protected String defaultEncoding = null
hibernate 的中文乱码解决方法 
hibernate 的中文乱码解决方法    
/* (non-Javadoc) 
hibernate 的中文乱码解决方法     * @see javax.servlet.Filter#init(javax.servlet.FilterConfig) 
hibernate 的中文乱码解决方法     
*/
 
hibernate 的中文乱码解决方法    
public void init(FilterConfig arg0) throws ServletException 
hibernate 的中文乱码解决方法        
// TODO Auto-generated method stub 
hibernate 的中文乱码解决方法
        this.filterConfig = arg0; 
hibernate 的中文乱码解决方法        
this.defaultEncoding = filterConfig.getInitParameter("defaultencoding"); 
hibernate 的中文乱码解决方法    }
 
hibernate 的中文乱码解决方法 
hibernate 的中文乱码解决方法    
/* (non-Javadoc) 
hibernate 的中文乱码解决方法     * @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest, javax.servlet.ServletResponse, javax.servlet.FilterChain) 
hibernate 的中文乱码解决方法     
*/
 
hibernate 的中文乱码解决方法    
public void doFilter( 
hibernate 的中文乱码解决方法        ServletRequest request, 
hibernate 的中文乱码解决方法        ServletResponse response, 
hibernate 的中文乱码解决方法        FilterChain chain) 
hibernate 的中文乱码解决方法        throws IOException, ServletException 

hibernate 的中文乱码解决方法        
// TODO Auto-generated method stub 
hibernate 的中文乱码解决方法
        request.setCharacterEncoding(selectEncoding(request)); 
hibernate 的中文乱码解决方法        chain.doFilter(request, response); 
hibernate 的中文乱码解决方法    }
 
hibernate 的中文乱码解决方法 
hibernate 的中文乱码解决方法 
hibernate 的中文乱码解决方法    
public void destroy() 
hibernate 的中文乱码解决方法         
hibernate 的中文乱码解决方法        
this.defaultEncoding = null
hibernate 的中文乱码解决方法        
this.filterConfig = null
hibernate 的中文乱码解决方法    }
 
hibernate 的中文乱码解决方法 
hibernate 的中文乱码解决方法 
hibernate 的中文乱码解决方法    
protected String selectEncoding(ServletRequest request) 
hibernate 的中文乱码解决方法 
hibernate 的中文乱码解决方法        
return this.defaultEncoding; 
hibernate 的中文乱码解决方法    }
 
hibernate 的中文乱码解决方法 
hibernate 的中文乱码解决方法}