mysql jsp中文问题

来源:互联网 发布:极限挑战跑男感情知乎 编辑:程序博客网 时间:2024/05/18 08:21

http://blog.vetcafe.net/blog.php?job=art&articleid=a_20050913_135101

http://www.blueidea.com/bbs/newsdetail.asp?id=1230482

从数据库中取出数据,因为在Mysql中的编码方式是"ISO-8859-1"所以转换

public String toShow(String temp) {
  if(temp == null || temp.trim().equals("")) {
  return "";
  }
  try {
  String str = new String(temp.trim().getBytes("8859_1"),"gb2312");
  str=Replace(str," "," ");
  str=Replace(str,"<","&lt;");
  str=Replace(str,">","&gt;");
  str=Replace(str,"/n","<br>");
  return str;
  }catch(Exception e) {
  return temp;
  }
}

相反,在得到数据后,往数据库加入的时候,就要把GB2312转化为ISO-8859-1格式

public String toStr(String temp) {
if(temp == null || temp.trim().equals("")) {
return "";
}
try {
String str = new String(temp.trim().getBytes("gb2312"),"8859_1");
str = Replace(str,"'","''");
return str;
}catch(Exception e) {
return temp;
}
}
原创粉丝点击