英文:
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编码运行。
- 在Windows 10的CMD中,JAR文件可以像这样运行:
> java -Dfile.encoding="UTF-8" -jar test.jar
然而,这个命令在PowerShell中无法运行,为什么我不知道,这是另一个问题。
- 如果你想在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.
- 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.
- If you want to run war file on tomcat server, in
{pathToTomcat}/bin
, opencatalina.bat
with editor.
add set "JAVA_OPTS=%JAVA_OPTS% -Djavax.servlet.request.encoding=UTF-8 -Dfile.encoding=UTF-8"
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论