JDBC Connection issue to Azure SQL Database

ghazzal jastaniah 51 Reputation points
2021-08-09T14:08:59.033+00:00

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 Database
Azure SQL Edge
Azure SQL Edge
An Azure service that provides a small-footprint, edge-optimized data engine with built-in artificial intelligence. Previously known as Azure SQL Database Edge.
45 questions
Azure Database for MySQL
Azure Database for MySQL
An Azure managed MySQL database service for app development and deployment.
710 questions
0 comments No comments
{count} votes

Accepted answer
  1. Oury Ba-MSFT 16,076 Reputation points Microsoft Employee
    2021-08-10T00:17:15.113+00:00

    Hi @ghazzal jastaniah 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

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful