簡體   English   中英

如何使用 JDBC 驅動程序連接到帶有重音名稱的 SQL Server?

[英]How to connect to SQL Server with accent in name using JDBC driver?

我一直在嘗試將 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 字符。 不在 Latin1 中的字符和某些特殊字符使用The Java™ Language Specification 的第 3.3 節中定義的 Unicode 轉義符在鍵和元素中表示。

通常在 Java 中有兩個選項:

  1. 明確保存在 ISO-8859-1 中。 這是一種非常脆弱的方法,因為它只需要編輯器自動“修復”事物並保存為 UTF-8 即可重新引入問題
  2. 對 ASCII 范圍之外的字符使用 Unicode 轉義,在本例中為

特別是對於 Spring,您有第三種選擇

  1. 切換到使用 YAML 配置文件,默認情況下為 UTF-8。

對於一般的 Java,但不適用於 Spring 應用程序屬性,您還可以選擇使用Properties.load(Reader)加載屬性文件,其中讀取器已使用文件的正確字符集構建。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM