jsp+mysql 插入的字符全是问号。

xyqsoft 2007-10-17 04:37:40
jsp+mysql 插入的汉字全是问号。
我从网上搜了很多,照着做了,但是没有解决,不知道怎么回事。
...全文
540 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
vincentsytu 2007-10-25
  • 打赏
  • 举报
回复
还要注意你的mysql数据库字段的编码问题.
wangqingyu0088 2007-10-22
  • 打赏
  • 举报
回复
严重同意火果龙
  • 打赏
  • 举报
回复
55555~~~~,还是错了,再改一下,这次是彻底检查过的,不会再错了:

jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=GBK

看来以后不能再粘别人的了。
  • 打赏
  • 举报
回复
不好意思,第二个复制楼上的,写错了,更正一下,应该是:

jdbc:mysql://localhost:3306/test&userUnicode=true&characterEncoding=GBK
  • 打赏
  • 举报
回复
jsp+mysql 乱码问题是极其复杂的,想想一个 JSP 的乱码就已经很复杂了,再加个 MySQL,

按以下的方法逐个排除:

1. 保证 MySQL 中能正确地插入中文字符,可以在 MySQL 的控制台下测试。
  若不能的话,找到 MySQL 程序目录中的 my.ini 文件,在 [mysqld] 和
  [mysql] 组中加上 default-character-set=GBK,重启服务,重建数据
  库和数据表就 OK 了,若不想重建,那就得一个字段一个字段的更改编码,
  相当麻烦的。

2. JDBC 连接字符串为:jdbc:mysql://localhost:3306/test&userUncode=true&characterEncoding=GBK

3. JSP 页面的编码是 GBK,并且 request 和 response 的编码也设成了 GBK。
  这种可以配置一个过滤器,6 楼已经给出了一个。

4. 如果数据是采用 GET 方式提效的话,那就更为复杂了,先将
  %TOMCAT_HOME%/conf/server.xml 中的两个 Connector 加上 URIEncoding="GBK"
  更改地址栏编码,默认是 ISO-8859-1 的。如果你使用的是 Firefox 浏览器,
  那就 OK 没问题了,如果是 IE,那还有大量的问题,需要将 GET 请求提交的
  中文字符串使用 java.net.URLEncoder 的 encode(str, "gbk") 进行 URL
  编码,再从接收方使用 java.net.URLDecoder 的 decode(str, "gbk") 进
  解码。

以上是我在 JSP 中乱码的解决方法,以供参考。
lanzhengwu 2007-10-19
  • 打赏
  • 举报
回复
如果只是显示出现问号的话,应该不是过滤器的问题
像这样:String url = "jdbc:mysql://localhost:3306/exercise?" +
"user=root&password=root&userUncode=true&characterEncoding=GB2312";//从数据库里直接加中文没有问题;要从页面上加就得这么弄了
hanxiaoyidi 2007-10-19
  • 打赏
  • 举报
回复
加个过滤器就可以了,你把下面的代码拷贝一下,放到你的工程里,然后把下面的配置拷到web.xml里就可以了.

package com.cafuc.filter;
import java.io.IOException;
import javax.servlet.*;
public class EncodingFilter implements Filter
{
protected String encoding;
protected FilterConfig filterConfig;
protected boolean ignore;
public EncodingFilter()
{
encoding = null;
filterConfig = null;
ignore = true;
}
public void destroy()
{
encoding = null;
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;
encoding = filterConfig.getInitParameter("encoding");
String value = filterConfig.getInitParameter("ignore");
if(value == null)
ignore = true;
else
if(value.equalsIgnoreCase("true"))
ignore = true;
else
if(value.equalsIgnoreCase("yes"))
ignore = true;
else
ignore = false;
}
protected String selectEncoding(ServletRequest request)
{
return encoding;
}
}

web.xml里的配置如下:
<filter>
<filter-name>Set Character Encoding</filter-name>
<filter-class>com.hwadee.filter.EncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>GBK</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>Set Character Encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

对了,放在<servlet></servlet>的前面哦!
祝你成功!有新的问题在说一下哈!
胡矣 2007-10-19
  • 打赏
  • 举报
回复
request.setCharacterEncoding("****") ;
保证程序的编码方式和数据库的编码方式相同 不发生字符集转换就不会出现乱码了
nemo0228 2007-10-18
  • 打赏
  • 举报
回复
看看你jsp的字符集,一般是utf-8的,转一下
<%@ page contentType="text/html; charset=gbk" language="java"%>
用这句就行
当然,如果你要在sql里执行的话,还需要将字符转成和你的数据库相同的字符集
CambridgeLove 2007-10-18
  • 打赏
  • 举报
回复
public class ConnectionDB {
static Connection conn = null;
static PreparedStatement pstmt = null;
public static Connection connDB(){
String driver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/exercise?" +
"user=root&password=root&userUncode=true&characterEncoding=GB2312";//从数据库里直接加中文没有问题;要从页面上加就得这么弄了,^_^
String userName = "root";
String password = "root";
try {
Class.forName(driver);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.err.println(e.getMessage());
}
try {
conn = DriverManager.getConnection(url);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.err.println(e.getMessage());
}
return conn;
}
wshsm 2007-10-17
  • 打赏
  • 举报
回复
首先先保证mysql里面建立的表编码指定为gbk或者utf-8
godenvoy 2007-10-17
  • 打赏
  • 举报
回复
在Tomcat中的server.xml中
<Connector port="80" maxHttpHeaderSize="8192"
maxThreads="150" minSpareThreads="25" maxSpareThreads="75"
enableLookups="false" redirectPort="8443" acceptCount="100"
connectionTimeout="20000" disableUploadTimeout="true"
URIEncoding="utf-8"(加入这句)
/>
然后写一个过滤器,过滤页面就可以啦

58,454

社区成员

发帖
与我相关
我的任务
社区描述
Java Eclipse
社区管理员
  • Eclipse
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧