Hibernate与Mysql数据库结合乱码问题解决

  Hibernate+MySQL5.0结合的默认配置,那么向数据库写入中文数据的时候会是乱码,且jsp页面显示中文的时候也有有乱码问题,很是烦人。
  经过查询及整理,处理方法如下:
  1、修改hibernate的配置文件,在url处添加绿色的一段:  
< hibernate-configuration >
< session-factory name ="myjava" >
   < property name ="hibernate.connection.driver_class" >org.gjt.mm.mysql.Driver </ property >
   < property name ="hibernate.connection.url" >jdbc:mysql://localhost:3306/myjava ?useUnicode=true&amp;characterEncoding=UTF-8 </ property >
   < property name ="hibernate.connection.username" >root </ property >
   < property name ="hibernate.connection.password" >test </ property >
   < property name ="connection.pool_size" >10 </ property >
   < property name ="hibernate.dialect" >org.hibernate.dialect.MySQL5Dialect </ property >
   < property name ="current_session_context_class" >thread </ property >
   < property name ="cache.provider_class" >org.hibernate.cache.NoCacheProvider </ property >
   < property name ="show_sql" >true </ property >
   < property name ="hbm2ddl.auto" >create </ property >
</ session-factory >
</ hibernate-configuration >
 
2、jsp页面设置语言,同时form表单使用post方式提交
< html >
< head >
< meta http-equiv ="Content-Type" content ="text/html;charset=UTF-8" >
< title >Event Manager </title>
</head>
< body >
< form method ="post" >
...
</form>
</body>
</html>
 
3、创建一个语言过滤servlet,用来处理请求和发送的字符编码
(1)CharacterEncodingFilter.java文件的源码如下:
import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServlet;

@SuppressWarnings( "serial")
public class CharacterEncodingFilter extends HttpServlet implements Filter {

     public CharacterEncodingFilter() {
   // TODO Auto-generated constructor stub
    }

     public void doFilter(ServletRequest arg0, ServletResponse arg1,
      FilterChain arg2) throws IOException, ServletException {
   //下面的两个编码根据你的项目所使用的中文字符集确定
  arg0.setCharacterEncoding( "UTF-8");
  arg1.setCharacterEncoding( "UTF-8");
  arg2.doFilter(arg0, arg1);
    }
}
 
(2)WEB-INF/web.xml文件修改如下:
<? xml version ="1.0" encoding ="UTF-8" ?>
< web-app xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance"
     xmlns ="http://java.sun.com/xml/ns/javaee"
     xmlns:web ="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
     xsi:schemaLocation ="http://java.sun.com/xml/ns/javaee [url]http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd[/url]"
     id ="WebApp_ID" version ="2.5" >
  
     < filter >
   < filter-name >CharacterEncodingFilter </ filter-name >
   < filter-class >events.CharacterEncodingFilter </ filter-class >
     </ filter >
     < filter-mapping >
   < filter-name >CharacterEncodingFilter </ filter-name >
   < url-pattern >/* </ url-pattern >
     </ filter-mapping >

     < welcome-file-list >
   < welcome-file >index.html </ welcome-file >
   < welcome-file >index.htm </ welcome-file >
   < welcome-file >index.jsp </ welcome-file > 
     </ welcome-file-list >
</ web-app >
 
  到此,hibernate和mysql结合的中文存储乱码问题,jsp页面显示中文问题就解决了。如果你的应用还是有乱码问题,那就要详细对应问题情况,然后再处理问题了。

本文出自 “我的技术Blog” 博客,转载请与作者联系!

你可能感兴趣的:(java,jsp,Hibernate,mysql,乱码)