'Unable to Connect to Azure SQL-Server: sun.security.validator.ValidatorException

Created an Azure SQL server machine and I am able to rdp as well as connect via the local SQL Server Mgt Studio client.

However I cannot connect to the same instance via Java code using the connection string further below.

  • I am using the latest JDBC Driver (Microsoft JDBC Driver 7.4)
  • I am using SQL Server 2017 Express
  • I am able to connect manually via SQL Server Mgt Studio client on local machine

Here is the error message:

com.microsoft.sqlserver.jdbc.SQLServerException: The driver could not establish a secure connection to SQL Server by using Secure Sockets Layer (SSL) encryption. Error: "sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target". ClientConnectionId:xxxxxxxxxxxxxxxxxx

Here is the connection string:

jdbc:sqlserver://Server=nn.nnn.nnn.nnn;Integrated Security=false;User ID=myusername;Password=mypassword

Thanks for your help!



Solution 1:[1]

I got this to work by changing in the connection string:

encrypt=true

into

encrypt=false

Not sure this is the best solution but at least I can carry on developement.

Solution 2:[2]

It seems to be caused by the driver not finding a certificate that it trusts. If you do have a relevant certificate installed and are working on your localhost (meaning that it's probably self-signed and therefore wouldn't be trusted otherwise), try adding this to the end of your connection string: trustServerCertificate=true

If it's a different machine you're working on, you may need to take a look at the validity of the certificate that it should be using.

Microsoft also now has more information about SSL connections to SQL Server here.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 ibexy
Solution 2 Myles