对于mysql,sqlserver,interbase等数据库的jdbc旧版本都存在这样的问题。你可以更新你的数据库驱动来达到目的,当然也可以用下面的手段:具体原因是jdbc驱动里面使用了native编码,而我们的java程序则是unicode编码,所以我们需要进行转换。String native2unicode(String s) {
return new String(s.getBytes("iso-8859-1"),"gbk")
}
native2unicode(rs.getString(1));当然在写入数据库的时候要进行相应的反向的unicode2native转换。
return new String(s.getBytes("iso-8859-1"),"gbk")
}
native2unicode(rs.getString(1));当然在写入数据库的时候要进行相应的反向的unicode2native转换。
String native2unicode(String s) {
return new String(s.getBytes("iso-8859-1"),"gbk")
}
native2unicode(rs.getString(1));这个也可以嵌在页面上...不过不是最好的办法...
private static String getC(String strvalue)
{ try{if(strvalue==null)
return null;
else{strvalue = new String(strvalue.getBytes("ISO8859_1"), "GBK");
return strvalue;}
}catch(Exception e){return null;}
}
%>
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*,java.util.*,java.text.*" %>
从页面取出的参数,经过上述getC处理后入库
str=new String(astr.getBytes("ISO8859_1"), "gb2312");
TOMCAT:str = new String(str.getBytes("8859_1"), "GB2312")
WebSphere:str = new String(s.getBytes("ISO-8859-1"), "gb2312")
依此类推!
你好好试试吧!:-)
对于要显示的页面,前面加<%@ page contentType="text/html; charset=GB2312" %>
对于要存入数据库的字符串变量,在往数据库里面写之前,用下面的方法转换一下;其他的不要管就OK了
<%!
public String getStr(String str){
try{
String temp_p=str;
byte[] temp_t= temp_p.getBytes("ISO8859-1");
String temp = new String(temp_t);
return temp;
}catch(Exception e){
return null; }
}
%>