在Jsp中操作MySql中文问题???

ienet 2003-05-05 05:47:20
为什么在Jsp中向MySql数据库中添加中文数据,MySql中显示的中文数据是乱码,在Jsp程序中读出的时候,也是乱码,通过内码转换同样如此?该怎样解决?
...全文
82 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhangsq 2003-05-13
  • 打赏
  • 举报
回复
连接数据库时:
tring ConnStr="jdbc:mysql://localhost:3306/flystar?useUnicode=true&characterEncoding=gb2312";
即可!我用的就是mysql
zheng_wei99 2003-05-13
  • 打赏
  • 举报
回复
我也遇到这样的问题。sigh,痛苦!
snicker 2003-05-13
  • 打赏
  • 举报
回复
jsp网页中加上:
<%@ page contentType="text/html;charset="gb2312" %>

数据库连接url后面加上
"?useUnicode=true&characterEncoding=gb2312"

jeoky 2003-05-13
  • 打赏
  • 举报
回复
1、在插入数据操作的页面不要引用<%@ page contentType="text/html;charset="gb2312" %>,那么MySql中显示的中文数据是正确的;
2、在页面显示数据时才用<%@ page contentType="text/html;charset="gb2312" %>;
3、对有中文的字段查询时,要将关键字作编码转换
例如:name="程序";
name=new String(name.getBytes("ISO8859_1"),"gb2312");
这样才能正确查询;
baosn 2003-05-12
  • 打赏
  • 举报
回复
在jsp网页中显示中文只要在jsp网页中加入如下语句:
<%@ page contentType="text/html;charset="gb2312" %>
向数据库中插入中文前使用如下函数转换一下:
public static String toChinese(String strvalue){
try{
if(strvalue==null)
return null;
else{
strvalue=new String(strvalue.getBytes("ISO8859_1"),"GBK");
return strvalue;
}
}catch(Exception e){
return null;
}
}
ienet 2003-05-12
  • 打赏
  • 举报
回复
第一种方法我试了一下,不行。
第二种还没试。
dcmj 2003-05-05
  • 打赏
  • 举报
回复
试一下:

1.
一般的,接收传过来的中文串
String str = new String(request.getParameter("str").getBytes("8859_1"));
2.
import="java.net.URLEncoder"
import="java.net.URLDecoder"
.......
传时
URLEncoder.encode(_str);
接时
String str = URLDecoder.decode(_str);

在tomcat下需如下接收
String str = URLDecoder.decode(new String(request.getParameter("_str").getBytes("8859_1")));

81,094

社区成员

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

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