question

ghazzaljastaniah-2345 avatar image
0 Votes"
ghazzaljastaniah-2345 asked pituach commented

JDBC Connection issue to Azure SQL Database

I am trying to connect my Azure SQL database to my Android App on Android Studio, but there seems to be a problem from the client side not being able to form a connection.

Initially I was using jtds-1.3.1.jar for the driver, but then I read somewhere that it is not being used or updated anymore so I am currently using mssql-jdbc-9.4.0.jre8

I am forming connection URL as stated in the documentation

  Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
  connectionURL = "jdbc:sqlserver://t;databaseName=;user=;password=@;encrypt=true;trustServerCertificate=false;hostNameInCertificate=*.database.windows.net;loginTimeout=30;";
 connection = DriverManager.getConnection(connectionURL);



But I am getting this error:

The driver could not establish a secure connection to SQL Server by using Secure Sockets Layer (SSL) encryption. Error: "Socket is closed".










azure-sql-databaseazure-database-mysqlazure-sqldatabase-edge
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

OuryBa-MSFT avatar image
1 Vote"
OuryBa-MSFT answered pituach commented

Hi @ghazzaljastaniah-2345 Thank you for posting your question. The client and server cannot negotiate a common encryption protocol if TLS 1.3 is enabled.
You can try specifying different SSL/TLS levels like
sslProtocol=TLSv1.2

The Microsoft JDBC Driver for SQL Server supports setting the SSL protocol via the connection string.

String conURL = "jdbc:sqlserver://localhost;userName=sa;password=PASSW0RD;database=master;sslProtocol=TLS";
SQLServerConnection con = (SQLServerStatement) DriverManager.getConnection(conURL);
Another way to set the default label is using a SQLServerDataSource object.

SQLServerDataSource ds = new SQLServerDataSource();
ds.setUser("sa");
ds.setPassword("PASSWORD");
ds.setServerName("localhost");
ds.setPortNumber(1433);
ds.setDatabaseName("master");
ds.setSSLProtocol("TLS");
SQLServerConnection con = (SQLServerConnection) ds.getConnection();
TLS, TLSv1, TLSv1.1, TLSv1.2 are the supported protocol labels. The value of the property is used as the protocol on the SSLContext.getInstance method. SSLContext.getInstance method might behave differently depending on the JVM. We recommend reading about this method and the protocol labels before using the sslProtocol property. The following table demonstrates the enabled protocols with Oracle, IBM, and SAP.

Protocol Label ORACLE IBM SAP
TLS TLSv1, TLSv1.1, TLSv1.2 TLSv1 TLSv1, TLSv1.1, TLSv1.2
TLSv1 TLSv1 TLSv1 TLSv1
TLSv1.1 TLSv1.1 TLSv1.1 TLSv1, TLSv1.1
TLSv1.2 TLSv1.2 TLSv1.2 TLSv1, TLSv1.1, TLSv1.2

Regards,
Oury

· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thank you for your help! Apparently I had to configure the SQL server as well to fix the issue.

1 Vote 1 ·

I converted your comment to answer, so the OP will be able to mark it and close the thread.

0 Votes 0 ·