Garbled characters appear when inserting data into MySql in the SSH framework

Source: Internet
Author: User
Document directory
  • List the solutions that can be found again:
  • 1. When connecting to the database
  • 2. Set the MySQL Character Set
  • Show variables like 'character _ SET _ % ';
  • Set all the displayed character_set _ ** to utf8, that is, set character_set_serve = 'utf8'
  • Show variables like '% collation % ';
  • Set all displayed items to the following: Set collation_server = 'utf8 _ general_ci'
  • 3. Modify the MySQL configuration file my. ini.
  • Default-character-set = utf8
  • 4. Create a filter class
  • Package com. broadengate. CRM. filter;
  • Import java. Io. ioexception;
  • Public class characterencodingfilter implements filter {
  • }
  • 5. modify the configuration file server. XML in Tomcat.
  • Change
  •  
  • 6. Configure spring filter in Web. xml
  • <Filter>
  • <Filter-Name> char </filter-Name>
  • <Filter-class> org. springframework. Web. Filter. characterencodingfilter </filter-class>
  • <Init-param>
  • <Param-Name> encoding </param-Name>
  • <Param-value> UTF-8 </param-value>
  • </Init-param>
  • </Filter>
  • <Filter-mapping>
  • <Filter-Name> char </filter-Name>
  • <URL-pattern>/* </url-pattern>
  • </Filter-mapping>

 

List the solutions that can be found again: 1. When connecting to the database, JDBC. properties: JDBC: mysql: // localhost: 3306/myproject? Useunicode = true & characterencoding = utf8
Applicationcontext. xml: JDBC: mysql: // localhost: 3306/myproject? Useunicode = true & amp; characterencoding = utf8

 

2. Set the MySQL Character Set
 show variables like'character_set_%';
Set all the displayed character_set _ ** to utf8, that is, set character_set_serve = 'utf8'
 show variables like '%collation%'; 
Set all displayed items to the following: Set collation_server = 'utf8 _ general_ci'

 

3. Modify MySQL configuration file my. inidefault-character-set = utf8

 

4. Create a filtering class package com. broadengate. CRM. filter; 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; public class characterencodingfilter implements filter {public void destroy (){
} Public void dofilter (servletrequest request, servletresponse response,
Filterchain chain) throws ioexception, servletexception {
Request. setcharacterencoding ("UTF-8 ");
Chain. dofilter (request, response );
} Public void Init (filterconfig arg0) throws servletexception {

} Add web. xml configuration
<Filter>
<Filter-Name> encodefilter </filter-Name>
<Filter-class> com. broadengate. CRM. Filter. characterencodingfilter </filter-class>
</Filter>
<Filter-mapping>
<Filter-Name> encodefilter </filter-Name>
<URL-pattern>/* </url-pattern>
</Filter-mapping>
5. modify the configuration file server. XML in Tomcat <connector Port = "8080" protocol = "HTTP/1.1"
Connectiontimeout = "20000"
Change redirectport = "8443"/> to <connector Port = "8080" protocol = "HTTP/1.1"
Connectiontimeout = "20000"
Redirectport = "8443" uriencoding = "UTF-8"/> 6. xml configuration spring filter <filter> <filter-Name> char </filter-Name> <filter-class> Org. springframework. web. filter. characterencodingfilter </filter-class> <init-param> <param-Name> encoding </param-Name> <param-value> UTF-8 </param-value> </init- param> </filter> <filter-mapping> <filter-Name> char </filter-Name> <URL-pattern>/* </url-pattern> </filter- mapping>
Related Article

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.