为什么更新后的数据库中文是乱码

yxqc 2004-10-15 03:47:30
我的数据是从mysql表里取出来的,取出来的数据是正常显示中文的。
可为什么更新后的数据库中文是乱码呢?
我把my.ini也改过了。可是还是不行啊。
C:\WINNT>type my.ini
[mysqld]
basedir=D:/mysql
datadir=D:/mysql/data
default-character-set= gbk

我的代码如下:

//取得参数:
String newsname = request.getParameter("newsname");
String newstitle = request.getParameter("newstitle");
String newscontent = request.getParameter("newscontent");

//编码转化
newsname = new String(newsname.getBytes("GB2312"));
newstitle = new String(newstitle.getBytes("GB2312"));
newscontent = new String(newscontent.getBytes("GB2312"));

try
{
con=getConnection();
stmt=con.createStatement();
String sql="update news set newsname ='"+newsname+"', newstitle = '"+newstitle+" ', newscontent = '"+newscontent+"'where Id ="+ii;
stmt.executeUpdate(sql);
closeAll(rs, stmt, con);
}
catch(SQLException e)
{
System.out.println(e.getMessage());
}

%>

...全文
88 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
dlinyo 2004-10-15
  • 打赏
  • 举报
回复
galewithwing(漂流的风)说的对。

newsname = new String(newsname.getBytes("GB2312"));
newstitle = new String(newstitle.getBytes("GB2312"));
newscontent = new String(newscontent.getBytes("GB2312"));
不需要
galewithwing 2004-10-15
  • 打赏
  • 举报
回复
那就是你更新部分程序的问题啦!
你的修改表单提交页面 在获取参数前用上一句:request.setCharacterEncoding("gb2312");
就好了
jerrymousenet2 2004-10-15
  • 打赏
  • 举报
回复
存入数据库之前,先转成UTF-8后,再存入数据库中。从数据库中,取出后再经过转换一下,即可正常。
yxqc 2004-10-15
  • 打赏
  • 举报
回复
当往数据库添加新的数据是好的。但是把数据从数据库里取出来更新之后就是乱码了。为什么会这样?
galewithwing 2004-10-15
  • 打赏
  • 举报
回复
问题是你怎么导入数据的?
如果通过程序到是很可能的
ejbcreate 2004-10-15
  • 打赏
  • 举报
回复
At the first line, try to add this sentence:

request.setCharacterEncoding("GB2312");
dropship 2004-10-15
  • 打赏
  • 举报
回复
有一个问题你要搞清楚,数据库中的中文是乱码吗?

81,092

社区成员

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

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