mysql乱码

框架中插入数据到mysql出现乱码

列出能找的解决办法:

1、连接数据库的时候jdbc.properties: jdbc:mysql://localhost:3306/myproject?useUnicode=true&characterEncoding=utf8
applicationContext.xml: jdbc:mysql://localhost:3306/myproject?useUnicode=true&characterEncoding=utf8

 

2、设置mysql字符集 show variables like'character_set_%';把显示的character_set_**全都设置成utf8,即是:set character_set_serve='utf8' show variables like '%collation%'; 把显示的都设置成为如下:set collation_server='utf8_general_ci'

 

3、修改mysql配置文件my.inidefault-character-set=utf8

 

4、建一个过滤类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 {

}}web.xml配置加
<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、修改tomcat中的配置文件server.xml<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />改为<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" URIEncoding="UTF-8"/>6、在web.xml配置spring过滤器<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>

你可能感兴趣的:(mysql乱码)