Why am I not able to establish a JDBC connection to my Microsoft SQL Server database using SSL encryption?

20 views (last 30 days)
I am trying to connect to a Microsoft SQL database using JDBC driver from the Database Explorer GUI or with the following command:
>> Connection=database(iDatabase,iUserID,iPassword,'Vendor','Microsoft SQL Server','Server',iServer);
The connection failed with the following error message:
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".
​However, I am able to connect to the same database using non-MATLAB tools. Why am I receiving this error and how can I resolve this?

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 31 Jul 2023
In order to connect to your database using SSL encryption, there are some additional parameters needed to be set.
(1) You may use the 'database' function with the following syntax to connect to your database from the MATLAB Command Line:
conn = database(databasename,username,password,driver,url)
Where the following 'url' can be used to establish the connection using SSL encryption: 
url = "jdbc:sqlserver://servername:portnumber; databaseName=databaseName; encrypt=true; trustServerCertificate=true"
(2) You may also connect to the database from the Database Explorer GUI, by adding the 'encrypt' and 'trustServerCertificate' properties under 'Connection Options' and setting their values to 'true'.
In addition, if you would like to setup operating system authentication (Windows authentication), you need to set the 'integratedSecurity' property to be true by either appending it to the 'url' or adding the property as a connection option. Please follow step 3 in the following document to configure the operating system authentication. 
https://www.mathworks.com/help/releases/R2023a/database/ug/microsoft-sql-server-jdbc-windows.html#bt5dht6
For more connection options, please refer to the Microsoft documentation below:
https://learn.microsoft.com/en-us/sql/connect/jdbc/connecting-with-ssl-encryption

More Answers (0)

Products


Release

R2023a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!