2
votes

I have the following issue. The java resultset is not showing Unicode (Chinese) characters, but showing all other characters. I am sure all characters are stored/showing properly from in Microsoft SQL Server (as nvarchar).

So it seems to be a retrieving issue. Here is the code:

protected String getStringValueNoNulls(ResultSet rs, String colName) {

        String ret = rs.getString(colName);

        ret = new String(ret.getBytes(), "UTF8");

        System.out.println(ret);

... Output for the print statement:

SO (SO in DB)

??? (张先生 in DB)

??????9999 ( 建国门外大街9999 in DB)

?? (北京 in DB)

100010 (100010 in DB)

It showing all English/ascii characters but not the Chinese characters. I noticed the number of Chinese characters is equal to the question marks it replaces with.

I have tried before just plain getString(), and now doing getBytes() for conversion both producing the same results.

Is something I am missing, or is it maybe an issue with driver? Please help.

----------------I Just added this as my 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;useUn‌​icode=true;characterEncoding=UTF-8";

Connection con = DriverManager.getConnection(connectionUrl);

Still getting the same questions marks for the Chinese characters.

Regards.

2
Does your console (System.out destination) actually support printing these characters? - Mark Rotteveel

2 Answers

1
votes

Ok, figured the solution. Its a problem with Java & utf8 encoding not being able to print & write.

(Not a driver issue) First you must use a print stream if you are outputting (file or console):

Output to Console:

String ret = rs.getString(colName);
PrintStream out = new PrintStream(System.out, false, "UTF8");  //This is the key
out.println(ret);

And to a 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);
  } finally {
    res.close();
  }
}
0
votes

add the connection must be like

connection = riverManager.getConnection("jdbc:sqlserver://190.128.4.195;databaseName=unicodedemo;user=ab;password=ab@Admin;useUnicode=true;characterEncoding=UTF-8");