2

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.

GreenLeaf
  • 31
  • 1
  • 5

2 Answers2

1

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();
  }
}
arghtype
  • 4,376
  • 11
  • 45
  • 60
GreenLeaf
  • 31
  • 1
  • 5
  • Thumbs up pls if this solution has helped you. – GreenLeaf Aug 27 '13 at 12:18
  • This didn't work for me. I am working on Kurdish Sorany text. I use Eclipse IDE. The problem was the console. I fixed the problem by following this : https://stackoverflow.com/questions/9180981/how-to-support-utf-8-encoding-in-eclipse – Dilman Salih May 18 '22 at 11:00
0

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");

Ashish Chaurasia
  • 1,747
  • 17
  • 23
  • Hi. Thank you for the reply. I added this: 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 the same questions marks for the Chinese characters. – GreenLeaf Aug 23 '13 at 15:24