mysql - decode character from unicode using java -
i unable insert chinese character mysql. though of doing this. have excel sheet have chinese characters. 秀昭
, on.
i got them converted unicode representations \uxxx
using below code got so, , stored in mysql.
private static string escapenonascii(string str) { list<string> arr = new arraylist<string>(); stringbuilder retstr = new stringbuilder(); (int = 0; < str.length(); i++) { int cp = character.codepointat(str, i); system.out.println("cp="+cp); int charcount = character.charcount(cp); if (charcount > 1) { += charcount - 1; // 2. if (i >= str.length()) { throw new illegalargumentexception("truncated unexpectedly"); } } if (cp < 128) { retstr.appendcodepoint(cp); } else { retstr.append(string.format("\\u%x", cp)); arr.add(string.format("\\\\u%x", cp)); } } return retstr.tostring(); }
the values have been stored properly. need display them back. when tried
system.out.println("\u8bf7\u5728\u6b64\u5904");
it gives me proper output like,
`请在此`
but when read db , did like
system.out.println(rs.getstring(1).trim().tostring() + " db");
it printed
`\u8bf7\u5728\u6b64\u5904`
what might problem? have missed anything? please help.
escaped characters processed prior compiling. store , retrieve data database, have consider 2 things: make sure data read had correct encoding. , when printing data correct encoding set. if read data on windows machine, posible have use cp* encodings. use inputstreamreader , set charset. have data in jvm. internal encoding utf-16. use type 4 jdbc, not have worry encoding, except database needs encoding capable store data. utf-8 or unicode trick. consult jdbc documentation properties set. have set encoding explicitly (jdbc:mysql://localhost:3306/?useunicode=yes&characterencoding=utf-8). when outputting data, output must have specific encoding. normally, jvm runs default system char set need one, example when rendering html file.
Comments
Post a Comment