日期:2014-05-20  浏览次数:20941 次

mysql + hibernate 出现的中文乱码问题
我在装mysql的时候,选择字符集时使用的gb2312
然后我用hibernate给MYSQL数据库中传入数据 
其中有一个字段存的是中文字符,结果在程序中显示的是中文乱码
我在hibernate中给这个字段的赋值语句是 user.setName("继续努力");
为什么存入到数据库中的数据库会是??两个问号呢 以前没碰到过这样的事情啊
高手说下

------解决方案--------------------
UTF-8
------解决方案--------------------
uft-8
------解决方案--------------------
在web。xml中配置过滤器
<filter>
<filter-name>Set Character Encoding</filter-name>
<filter-class>Constant.SetCharacterEncodingFilter</filter-class>
 
<init-param>
<param-name>encoding</param-name>
<param-value>GB2312</param-value>
</init-param>
<init-param>
<param-name>ignore</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>Set Character Encoding</filter-name>
<servlet-name>action</servlet-name>
</filter-mapping>
<filter-mapping>
<filter-name>Set Character Encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping> 


Constant.SetCharacterEncodingFilter是要配置的过滤器类
源码

package Constant;

import javax.servlet.*;
import java.io.IOException;

public class SetCharacterEncodingFilter implements Filter {
protected String encoding = null;
protected FilterConfig filterConfig = null;
protected boolean ignore = true;
public void destroy() {
this.encoding = null;
this.filterConfig = null;
}
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain)
throws IOException, ServletException {
// Conditionally select and set the character encoding to be used
if (ignore || (request.getCharacterEncoding() == null)) {
String encoding = selectEncoding(request);
if (encoding != null)
request.setCharacterEncoding(encoding);
}
// 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");
String value = filterConfig.getInitParameter("ignore");
if (value == null)
this.ignore = true;
else if (value.equalsIgnoreCase("true"))
this.ignore = true;
else if (value.equalsIgnoreCase("yes"))
this.ignore = true;
else
this.ignore = false;
}
protected String selectEncoding(ServletRequest request) {
return (this.encoding);
}
}



------解决方案--------------------
这要看你的MySQL的my.ini文件的设置了
要确定你的字符编码是gb2312