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

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

        分类:            SSH有关 200人阅读 评论(0) 收藏 举报

目录(?)[+]

  1. 再次列出能找的解决办法:
  2. 连接数据库的时候
  3. jdbc.properties: jdbc:mysql://localhost:3306/myproject?useUnicode=true&characterEncoding=utf8 applicationContext.xml: jdbc:mysql://localhost:3306/myproject?useUnicode=true&characterEncoding=utf8
  4. 设置mysql字符集
  5. show variables like'character_set_%';
  6. 把显示的character_set_**全都设置成utf8,即是:set character_set_serve='utf8'
  7. show variables like '%collation%';
  8. 把显示的都设置成为如下:set collation_server='utf8_general_ci'
  9. 修改mysql配置文件my.ini
  10. default-character-set=utf8
  11. 建一个过滤类
  12. package com.broadengate.crm.filter;
  13. import java.io.IOException;
  14. import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse;
  15. public class CharacterEncodingFilter implements Filter {
  16. public void destroy() {      }
  17. public void doFilter(ServletRequest request, ServletResponse response,                              FilterChain chain) throws IOException, ServletException {                                request.setCharacterEncoding("utf-8");                                  chain.doFilter(request, response);       }
  18. public void init(FilterConfig arg0) throws ServletException {        }
  19. }
  20. web.xml配置加      encodefilter         com.broadengate.crm.filter.CharacterEncodingFilter                     encodefilter             /*    
  21. 修改tomcat中的配置文件server.xml
  22. 改为
  23. 在web.xml配置spring过滤器
  24. char
  25. org.springframework.web.filter.CharacterEncodingFilter
  26. encoding
  27. UTF-8
  28. char
  29. /*

再次列出能找的解决办法:

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.ini

default-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>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值