【求助】JSP+MySQL 中文编码问题
在MySQL中用sql语句插入就可以正常插中文,在JSP中也可以显示中文,可是在JSP往MySQL里插数据的时候数据库中显示的就是???,我甚至尝试重新安装过MySQL,安装的时候选择的也是gb2312,数据库和表我也设置的是gb2312。我看了很多这方面的帖子,问题依然没有得到解决,请求CSDN的朋友帮助,谢谢大家。代码如下:
<%@page contentType= "text/html; charset=gb2312 " language= "java "%>
<%@page pageEncoding= "gb2312 "%>
<%@page import= "java.sql.* "%>
<html>
<jsp:useBean id= "dbm " scope= "session " class= "web.dbm "/>
<jsp:setProperty name= "dbm " property= "* "/>
<jsp:useBean id= "sqlm " scope= "session " class= "web.sqlm "/>
<jsp:setProperty name= "sqlm " property= "* "/>
<meta http-equiv= "Content-Type " content= "text/html; charset=gb2312 ">
<head>
<title> 提交成功 </title>
</head>
<body bgcolor= "#ffffff ">
<%!String gsmc;%>
<%!String lxr;%>
<%!String lxdh;%>
<%!String lxcz;%>
<%!String email;%>
<%!String lybt;%>
<%!String lynr;%>
<%
gsmc = new String(request.getParameter( "gsmc ").getBytes( "ISO-8859-1 "));
// String temp_p=request.getParameter( "gsmc ");
// byte[] temp_t=temp_p.getBytes( "ISO-8859-1 ");
// String temp=new String(temp_t);
lxr = new String(request.getParameter( "lxr ").getBytes( "ISO-8859-1 "));
lxdh = new String(request.getParameter( "lxdh ").getBytes( "ISO-8859-1 "));
lxcz = new String(request.getParameter( "lxcz ").getBytes( "ISO-8859-1 "));
email = new String(request.getParameter( "email ").getBytes( "ISO-8859-1 "));
lybt = new String(request.getParameter( "lybt ").getBytes( "ISO-8859-1 "));
lynr = new String(request.getParameter( "lynr ").getBytes( "ISO-8859-1 "));
try {
dbm.getConnection();
out.print(gsmc);
out.print(lxr);
out.print(lxdh);
out.print(lxcz);
out.print(email);
out.print(lybt);
out.print(lynr);
sqlm.setSqlStr( "insert into pic values( '我 ') ");
sqlm.executeUpdate();
sqlm.setSqlStr( "insert into liuyan values( ' " + gsmc + " ' " + ", ' " + lxr + " ', ' " + lxdh + " ', ' " + lxcz + " ', ' " + email + " ', ' " + lybt + " ', " + " ' " + lynr + " ' " + ") ");
sqlm.executeUpdate();
out.println( " <font size=3 color=blue> 提交成功,稍后返回...... </font> <meta http-equiv= 'refresh ' content= '2;url=/car/lx.html '> ");
}
catch (Exception e) {
out.print(e.toString());
}
%>
</body>
</html>
----------bean里的直连代码:
private String driverName = "com.mysql.jdbc.Driver ";
private String url = "jdbc:mysql://127.0.0.1:3306/car??useUnicode=true&characterEncoding=gb2312 ";
private String user = "root ";
private String password = "123 ";
[解决办法]
环境:
WindowsXP中文
Eclipse3.2.1+Myeclipse5.1.0GA
Tomcat5.5
JDK1.5.0
Hibernate3.1
Mysql5.0+ mysql-connector-java-5.0.4-bin.jar
方案:
1.集成开发环境Eclipse中设置文本文件存储编码为UTF-8。
//我想是因为….如果所做工程项目最终要在别的版本操作系统的服务器上跑,这里需要设置(未经证实)
2.数据库mysql,默认编码使用utf8;
并且创建数据库时在语句后面追加DEFAULT CHARSET=utf8;set names utf8;
//如果数据库默认编码是utf8,那这个也不是必需的吧(未经证实)
3.跟数据库连接的URL:Hibernate URL:
jdbc:mysql://127.0.0.1:3306/addressbook?useUnicode=true&characterEncoding=utf8
//如果数据库默认编码是utf8,那这个也不是必需的吧(未经证实)
4.使用过滤器,过滤器文件代码见后面附1。
<filter>
<filter-name> Set Character Encoding </filter-name>
<filter-class> org.biti.filters.SetCharacterEncodingFilter </filter-class>
<init-param>
<param-name> encoding </param-name>
<param-value> UTF-8 </param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name> Set Character Encoding </filter-name>
<url-pattern> /* </url-pattern>
</filter-mapping>
//过滤的是post还是get还没弄明白,据说只过滤器中一个,另一个见5。
5.修改Tomcat配置文件server.xml中Connector部分
<Connector port= "80 "
maxHttpHeaderSize= "8192 "
maxThreads= "150 " minSpareThreads= "25 " maxSpareThreads= "75 "
enableLookups= "false " redirectPort= "8443 " acceptCount= "100 "
connectionTimeout= "20000 " disableUploadTimeout= "true " />
加入URIEncoding= "UTF-8 "一项。
//我现在这个没乱码的就没有设置….
附1:SetCharacterEncodingFilter.java(可在Tomcat示例源码中找到)
package org.biti.filters;
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 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 {
if (ignore || (request.getCharacterEncoding() == null)) {
String encoding = selectEncoding(request);
if (encoding != null)
request.setCharacterEncoding(encoding);
}
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);
}
}
有问题多问google
[解决办法]
用过滤器,,同楼上的.
[解决办法]
我也出现过这个问题
最后还是没有解决,
并不是每次都这样,因为在同样的环境我做过其他的项目,正常。
至今不明白why?我也搜过 google baidu.他们都认为是编码问题。
还有这个编码问题不是乱码。就是问号,不等于乱码。在日志里面能看到,正常输出。
帮顶