jdbc连接字符集为us7ascii的oracle数据库乱码解决办法

原文地址: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编码的字符串。

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值