Solve Chinese problems of hibernate + MySql

Source: Internet
Author: User

There are many articles in this regard on the Internet, which have been resolved according to the above, but I modified the MySQL configuration file my. ini. I feel that it is not very good at deployment.

Here is another solution (mysql-4.1.11). Earlier MySQL versions do not seem to have any problem with Chinese (mysql-4.0.17 ).

1. Add a filter to set character set encoding to UTF-8.
Modify web. xml:

<Filter>
<Filter-Name> setcharacterencoding </filter-Name>
<Filter-class> hxz. Filter. setencodingfilter </filter-class>
<Init-param>
<Param-Name> encoding </param-Name>
<Param-value> UTF-8 </param-value>
</Init-param>
</Filter>
 
<Filter-mapping>
<Filter-Name> setcharacterencoding </filter-Name>
<URL-pattern>/* </url-pattern>
</Filter-mapping>

Create a filter:
Package hxz. filter;

Import java. Io. ioexception;

Import javax. servlet .*;

Public class setencodingfilter implements filter {
 
// Default character encoding
String defaultencoding = "UTF-8 ";
 
Public void Init (filterconfig config) throws servletexception {
String encoding = config. getinitparameter ("encoding ");
If (encoding! = NULL ){
Defaultencoding = encoding;
}
}

Public void dofilter (servletrequest request, servletresponse response, filterchain chain) throws ioexception, servletexception {
Request. setcharacterencoding (defaultencoding );
Chain. dofilter (request, response );
}

Public void destroy (){
Defaultencoding = NULL;
}

}

2. Modify the hibernate configuration file:
<Property name = "url">
<Value> JDBC: mysql: // localhost: 3306/test? Useunicode = true & amp; characterencoding = UTF-8 </value>
</Property>
Note the following: useunicode = true & amp; characterencoding = UTF-8, in XML & to change to; amp;

3. When creating a table in the new MySQL version, you can select a character set to set it to UTF-8.
In JSP or servlet, change character set to UTF-8.

4. trying...

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.