原来项目中oracle数据库一直是US7ASCII,我新项目对接的时候,查询以及插入中文,出现乱码问题。
暂时未能解决此问题,最终决定每次转码:
查询的时候:
1 List<Record> list = Db.use("oracle").find("select * from order order by id desc"); 2 for (Record record : list) { 3 String str = record.getStr("department"); 4 String ss = ""; 5 if(str!=null) 6 ss = ArticleUtil.convert(str,"gbk"); 7 record.set("department",ss ); 8 }
关于转码的操作:
1 public static String convert(String str,String charsetName){ 2 String result = "未识别"; 3 try { 4 result = new String(str.getBytes("ISO-8859-1"),charsetName); 5 } catch (UnsupportedEncodingException e) { 6 e.printStackTrace(); 7 } 8 return result; 9 }
插入和更新的时候,处理中文:
public void testSave(){ String username = getPara("username"); String password = getPara("password"); String department = getPara("department"); try { department=new String(department.getBytes("gbk"),"iso-8859-1"); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } Record r = new Record().set("department",department) .set("username",username).set("password",password); Db.use("oracle").save("user", r); renderJson(); }
也就是说,当读取的时候,从数据中得到中文字符,然后以iso-8859-1 encode,再以gbk decode来正确显示中文,
当写入的时候,需要将中文字符 以gbk encode,再以iso-8859-1 decode,写入数据库。