mysql 中文问题。经常遇到这个问题但以前从来都不总结,每次碰到问题再出解决问题,再到网上出搜了,每次真的花了好多冤枉时间了,今天在这里总结一下了;解决问题的方案如下:
我用的是utf-8编码,在这里首先必须强调我们设置的编码方式一样了,都为utf-8;页面的也是utf-8;
1,在连接数据库的时候代码如下:
2:用过虑器来解决乱码问题,本人觉的非常好用了,并且复用性强,在JAVA编程里面实现了java 的一次编译,到处运行了,呵呵,夸张点了,但是真的好用了,过虑器为servelt,这个必须有一点serlvet经常知识了,我现在讲的是jsp+javabean+servlet经典的MVC了,基于这个基础上了,呵呵,servlet代码如下:
3,剩下一步就是在web.xml配置如下:
这个参数值可以变成 <param-value> gbk等任意的编码方式,但是个人认为utf-8比较好,因为呵呵网上高手说的了;呵呵,乱码问题高定了,这中方法对struts+spring+hibernate等任意框加通用啊,呵呵,不的确不错啊!再次强调我们的mysql的编玛方式必须设置为utf-8。设置如下:就是在mysql configuration character set 里面配置成 utf-8
我用的是utf-8编码,在这里首先必须强调我们设置的编码方式一样了,都为utf-8;页面的也是utf-8;
1,在连接数据库的时候代码如下:
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(
"jdbc:mysql://localhost/test?useUnicode=true&characterEncoding=utf-8",
"root", "123456");
2:用过虑器来解决乱码问题,本人觉的非常好用了,并且复用性强,在JAVA编程里面实现了java 的一次编译,到处运行了,呵呵,夸张点了,但是真的好用了,过虑器为servelt,这个必须有一点serlvet经常知识了,我现在讲的是jsp+javabean+servlet经典的MVC了,基于这个基础上了,呵呵,servlet代码如下:
public class SetCharacterEncodingFilter implements Filter {
protected String encoding = null;
protected FilterConfig filterConfig = null;
public void destroy(){
this.encoding = null;
this.filterConfig = null;
}
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain)
throws IOException, ServletException{
// Select and set (if needed) the character encoding to be used
String encoding = selectEncoding(request);
if (encoding != null)
request.setCharacterEncoding(encoding);
//System.out.println("Enter doFilter......");
// Pass control on to the next filter
chain.doFilter(request, response);
}
public void init(FilterConfig filterConfig) throws ServletException{
this.filterConfig = filterConfig;
this.encoding = filterConfig.getInitParameter("encoding");
}
protected String selectEncoding(ServletRequest request){
return (this.encoding);
}
}
3,剩下一步就是在web.xml配置如下:
<filter>
<filter-name>myfilter</filter-name>
<filter-class>
org.servlet.SetCharacterEncodingFilter
</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
这个参数值可以变成 <param-value> gbk等任意的编码方式,但是个人认为utf-8比较好,因为呵呵网上高手说的了;呵呵,乱码问题高定了,这中方法对struts+spring+hibernate等任意框加通用啊,呵呵,不的确不错啊!再次强调我们的mysql的编玛方式必须设置为utf-8。设置如下:就是在mysql configuration character set 里面配置成 utf-8