-1

I got a very strange problem here.

I need to read data from SQL Server (ver. 2016). A name like O'Neal.

When I load the String in my IDE/TextEditor, O'Neal could be read correctly as O'Neal.

But when I package my program as jar or war file, O'Neal is loaded like this O��Neal.

I have no idea how does this happend. Any suggestion would be appreciated. Thanks.

  • What do you mean by loaded? Are you trying to print that value somewhere? If the actual text is encoded with UTF-8, make sure you use the same encoding while displaying the text. – Shashank Kadne Oct 01 '20 at 05:30
  • @ShashankKadne Sorry about lack of the info. Yes, I print them in console. Actually, I'm not sure the encoding, I checked `SELECT SERVERPROPERTY('Collation')`, it returns SQL_Latin1_General_CP1_CI_AS – zhengchengc Oct 01 '20 at 05:52

1 Answers1

0

Refer to this question

Like @Arvind said, it runs correctly in IDE/Editor, because it's already encoded as UTF-8.

However, the jar or tomcat isn't, so we have to make sure it runs with encoding UTF-8.

  1. In Windows 10 CMD, the jar file could run like this:

> java -Dfile.encoding="UTF-8" -jar test.jar

However this command couldn't run in PowerShell, I don't know why it's another issue.

  1. If you want to run war file on tomcat server, in {pathToTomcat}/bin, open catalina.bat with editor.

add set "JAVA_OPTS=%JAVA_OPTS% -Djavax.servlet.request.encoding=UTF-8 -Dfile.encoding=UTF-8"