当前位置: 代码迷 >> Java Web开发 >> mysql不能存入汉字?解决方法
  详细解决方案

mysql不能存入汉字?解决方法

热度:217   发布时间:2016-04-17 17:20:51.0
mysql不能存入汉字?!!
各位大哥大姐好,我现在可以直接在数据库里面写汉字,但通过表单得到的汉字确无法在数据库正常显示,都是“???”形式,通过注册用户名“小明”进入数据库后显示??? 但在用“小明”还是可以登陆的,我jsp程序是utf8形式的。数据库设置utf8以及gb2312都不行啊...求解释

------解决方案--------------------

如上图所示,重新设置编码后,需要重新建数据库。
------解决方案--------------------
数据库既然能直接插入汉字,说明数据库编码是没有问题的,而是JSP传递过程中出现了乱码问题。这种情况,要加个filter进去。操作如下:
首先,增加一个filter类:
Java code
package com;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 EncodingFilter implements Filter {    private String encoding;    private FilterConfig filterConfig = null;    public void destroy() {        this.encoding = null;        this.filterConfig = null;    }    public void doFilter(ServletRequest request, ServletResponse response,            FilterChain chain) throws IOException, ServletException {        request.setCharacterEncoding(this.encoding);        chain.doFilter(request, response);    }    public void init(FilterConfig filterConfig) throws ServletException {        this.encoding = filterConfig.getInitParameter("encoding");    }}
  相关解决方案