问号和菱形,在从SQL Server读取nvarchar时出现问题。

huangapple go评论68阅读模式
英文:

Question mark with diamond when read nvarchar from SQL Server

问题

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 happen. Any suggestion would be appreciated. Thanks.

英文:

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.

答案1

得分: 0

参考这个问题

像@Arvind所说,在IDE/编辑器中可以正确运行,因为它已经以UTF-8编码。

然而,JAR文件或Tomcat不是这样,所以我们必须确保它以UTF-8编码运行。

  1. 在Windows 10的CMD中,JAR文件可以像这样运行:

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

然而,这个命令在PowerShell中无法运行,为什么我不知道,这是另一个问题。

  1. 如果你想在Tomcat服务器上运行WAR文件,在{pathToTomcat}/bin中,用编辑器打开catalina.bat

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

英文:

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"

huangapple
  • 本文由 发表于 2020年10月1日 13:21:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/64149514.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定