0

I am new to Android app development and I'm trying to connect to an Azure SQL DB with the native MS JDBC driver (sqljdbc4.jar) and I keep on getting the following error:

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

i am able to connect to the DB with the same connectionstring with squirrel though.

i use android studio 2.1.3 and i am really at the end of my wits... here is the code:

DriverManager.registerDriver(new com.microsoft.sqlserver.jdbc.SQLServerDriver());
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String username = "<>";
String password = "<>";
java.sql.Connection DbConn = DriverManager.getConnection("jdbc:sqlserver://<myserver>:1433;databaseName=thumbsupp;user=" + username + ";password=" + password + ";encrypt=true;trustServerCertificate=true;hostNameInCertificate=*.database.windows.net;loginTimeout=30;");

i even tried to open a socket on port 1433 in code:

SocketFactory sf = SSLSocketFactory.getDefault();
SSLSocket socket = (SSLSocket) sf.createSocket("<myserver>", 1433);
HostnameVerifier hverify = HttpsURLConnection.getDefaultHostnameVerifier();
SSLSession ssoc = socket.getSession();

but to no prevail although I get true when calling

hverify.verify();

Am I missing some configurations from Android Studio?

edit: if a check the enabled protocols on the socket:

socket.getEnabledProtocols();

the only protocols i get back are TLS1.0, TLS1.1 and TLS1.2 ??

1 Answers1

0

It seems that there are many SO/MSDN threads which have the same issue. Meanwhile, a workaround way is that using jTDS Driver instead of MS SQL Server JDBC Driver.

Please refer to these threads below to solve your issue.

  1. Sql Database connect Using the JDBC Driver with android
  2. https://social.msdn.microsoft.com/Forums/sqlserver/en-US/4799819a-27fd-4292-a3d8-f114207c20b2/using-the-jdbc-driver-with-android?forum=sqldataaccess
  3. https://blogs.msdn.microsoft.com/nickhodge/2010/01/25/jdbc-to-sql-azure/

Hope it helps.

Community
  • 1
  • 1
Peter Pan
  • 23,476
  • 4
  • 25
  • 43