如何使用JDBC驱动程序连接到名称带有重音的SQL Server



我一直在尝试将Spring Boot应用程序连接到本地SQL Server,但由于服务器名称包含重音字符(即É(,连接协议似乎不允许这样做。

我尝试过指定useUnicodecharacterEncoding属性,但到目前为止没有任何效果。

这是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字符。非拉丁字符1和某些特殊字符使用中定义的Unicode转义在键和元素中表示Java的第3.3节™语言规范。

在Java中通常有两个选项:

  1. 在ISO-8859-1中显式保存。这是一种非常脆弱的方法,因为它只需要一个编辑器自动"修复"并保存为UTF-8就可以重新引入问题
  2. 对ASCII范围之外的字符使用Unicode转义,在本例中为u00c9

特别是对于Spring,您有第三个选项

  1. 切换到使用YAML配置文件,默认情况下为UTF-8

对于一般的Java,但对于Spring应用程序属性,您还可以选择使用Properties.load(Reader)加载属性文件,其中读取器是用文件的正确字符集构建的。

相关内容

最新更新