原文地址:http://www.databaseskill.com/2247741/
我的jsp编码是utf-8,数据库oracle用的编码是us7ascii,在使用weblogic8的时候,使用在网上找到的方案能很好地解决乱码问题:
str = new String(rs.getString(i).getBytes("ISO-8859-1","GBK");
后来用weblogic 12c,发现即使用了上面的转换,也是得到乱码。仔细查看发现用getString取出来的字符已经是utf-8编码,gbk字符转换到utf-8会丢失,在反向转换也无济于事。于是上网乱找,看到了下面这些:
Chinese read from the database and displayed in the pages of GBK. If the database is stored in the font encoding is UTF-8, in the JSP page must use the statement str = new String (rs.getBytes (1), "UTF-8") or str = new String (rs.getString (1) . getBytes ("ISO-8859-1"), "UTF-8"), in order to correctly display Chinese. If the database is stored in a the GBK data, then the JSP should use str = new String (rs.getBytes (1), "GBK") or str = new String (rs.getString (1). GetBytes ("ISO-8859 -1 ")," GBK ") to display the correct Chinese.
(2) If the page is UTF-8, can not be directly displayed correctly GBK, a two-step conversion, str = new String (rs.getBytes (1), "GBK"), then str = new String the (str.getBytes ("UTF- 8 ")," GBK ") can correctly display Chinese. If kept in the database is UTF-8 directly with str = new String (rs.getBytes (1), "GBK") or str = new String (rs.getString (1). GetBytes ("ISO-8859-1" ), "GBK") can display Chinese.
重点在于不是用rs.getString()来取得字段,而是rs.getBytes()得到字节数组,然后直接生成gbk编码的字符串。