我一直在尝试将 Spring Boot 应用程序连接到我的本地 SQL Server,但由于服务器名称包含重音字符(即É
),连接协议似乎不允许这样做。
我曾尝试指定useUnicode
和characterEncoding
属性,但到目前为止没有任何效果。
这是application.properties
文件:
spring.datasource.driver-class-name=com.microsoft.sqlserver.jdbc.SQLServerDriver
#connection using Windows Authentication
spring.datasource.url=jdbc:sqlserver://FLÉK\\MSSQLSERVER02;databasename=MyDatabase;integratedSecurity=true;useUnicode=true;characterEncoding=UTF-8
spring.datasource.username=usname
spring.datasource.password=somepassword
和错误信息:
com.microsoft.sqlserver.jdbc.SQLServerException: The connection to the host FL�K, named instance mssqlserver02 failed. Error: "java.net.UnknownHostException: FL�K". Verify the server and instance names and check that no firewall is blocking UDP traffic to port 1434. For SQL Server 2005 or later, verify that the SQL Server Browser Service is running on the host.
我无法更改服务器名称,因为这需要重新安装服务器本身。任何想法我应该在哪个级别寻找解决方案,或者我应该在哪里启用unicode的这种用法?
Java.properties
文件 - 默认情况下 - 预计采用 ISO-8859-1 字符集,但看起来您保存为 UTF-8。见Properties.load(InputStream)
:
从输入字节流中读取属性列表(键和元素对)。输入流采用简单的面向行的格式,如 中所述,
load(Reader)
并假定使用 ISO 8859-1 字符编码;即每个字节是一个 Latin1 字符。不在 Latin1 中的字符和某些特殊字符使用The Java™ Language Specification 的第 3.3 节中定义的 Unicode 转义符在键和元素中表示。
通常在 Java 中有两个选项:
\u00c9
。特别是对于 Spring,您有第三种选择
对于一般的 Java,但不适用于 Spring 应用程序属性,您还可以选择使用 加载属性文件Properties.load(Reader)
,其中读取器已使用文件的正确字符集构建。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句