sql server - Java Resultset Not Showing Unicode (Chinese) Characters, But Showing as Question Marks -
i have following issue. java resultset not showing unicode (chinese) characters, showing other characters. sure characters stored/showing in microsoft sql server (as nvarchar).
so seems retrieving issue. here code:
protected string getstringvaluenonulls(resultset rs, string colname) {
string ret = rs.getstring(colname); ret = new string(ret.getbytes(), "utf8"); system.out.println(ret);
... output print statement:
so (so in db)
??? (张先生 in db)
??????9999 ( 建国门外大街9999 in db)
?? (北京 in db)
100010 (100010 in db)
it showing english/ascii characters not chinese characters. noticed number of chinese characters equal question marks replaces with.
i have tried before plain getstring(), , doing getbytes() conversion both producing same results.
is missing, or maybe issue driver? please help.
----------------i added connection, didn't help:
class.forname("com.microsoft.sqlserver.jdbc.sqlserverdriver");
string connectionurl = "jdbc:sqlserver://127.0.0.1:1433;database=mydb;user=myuser;password=mypass;useunicode=true;characterencoding=utf-8";
connection con = drivermanager.getconnection(connectionurl);
still getting same questions marks chinese characters.
regards.
ok, figured solution. problem java & utf8 encoding not being able print & write.
(not driver issue) first must use print stream if outputting (file or console): //console
` string ret = rs.getstring(colname);
printstream out = new printstream(system.out, false, "utf8"); //this key out.println(ret); `
//file
private static void writeutf8tofile(file file, boolean append, string data) throws ioexception { boolean skipbom = append && file.isfile() && (file.length() > 0); closer res = new closer(); try { outputstream out = res.using(new fileoutputstream(file, append)); writer writer = res.using(new outputstreamwriter(out, charset .forname("utf-8"))); if (!skipbom) { writer.write('\ufeff'); } writer.write(data); } { res.close(); } }
Comments
Post a Comment