简体   繁体   中英

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

I've been trying to connect a Spring Boot application to my local SQL Server, but since the server name contains an accented character (namely É ), the connection protocol doesn't seem to allow it.

I have tried specifying the useUnicode and characterEncoding properties, but nothing has worked so far.

Here is the application.properties file:

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

And the error message:

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.

I can't change the server name since that would require re-installing the server itself. Any ideas what level I should be looking for the solution at, or where should I enable this usage of unicode?

Java .properties file are - by default - expected to be in ISO-8859-1 character set, but it looks like you saved as UTF-8. See Properties.load(InputStream) :

Reads a property list (key and element pairs) from the input byte stream. The input stream is in a simple line-oriented format as specified in load(Reader) and is assumed to use the ISO 8859-1 character encoding; that is each byte is one Latin1 character. Characters not in Latin1, and certain special characters, are represented in keys and elements using Unicode escapes as defined in section 3.3 of The Java™ Language Specification .

You have two options in Java in general:

  1. Explicitly save in ISO-8859-1. This is a very brittle approach, as it only takes an editor automatically 'fixing' things and saving as UTF-8 to reintroduce the problem
  2. Use Unicode escapes for characters outside the ASCII range, in this case .

For Spring specifically, you have a third option

  1. Switch to using YAML configuration files, which are UTF-8 by default.

For Java in general, but not for Spring application properties, you also have the option to load the properties-file using Properties.load(Reader) , where the reader has been constructed with the right character set of the file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM