13

I used this code to create a connection to SQL Server.

String connectionUrl = "jdbc:sqlserver://IP:1433;" +
        "databaseName=db;user=db;password=pwd";
    Connection con = null;
  try {
     // Establish the connection.
     Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
     con = DriverManager.getConnection(connectionUrl);
        return "true";
  }

  // Handle any errors that may have occurred.
  catch (Exception e) {
     e.printStackTrace();
  }

but i got this error:

com.microsoft.sqlserver.jdbc.SQLServerException: The driver could not establish a secure connection to SQL Server by using Secure Sockets Layer (SSL) encryption. Error: "Socket closed". ClientConnectionId:5975fad5-8f8d-496a-a2bb-bff3a8d1a755

Can anybody help me? Thanks in advance

5
  • what is con? Have you tried calling con.Open() or equivalent? Commented Sep 4, 2013 at 18:09
  • I'm sorry. i forgot to write the definition of variables.i'll edit the question Commented Sep 4, 2013 at 18:12
  • I think there is not IP in a connection string Commented Sep 4, 2013 at 18:14
  • I just write IP as an example :). assume it is localhost or 127.0.0.1 or any other host Commented Sep 4, 2013 at 18:16
  • @MojtabaMahamed I'm sorry. My fault :D Commented Sep 4, 2013 at 18:21

2 Answers 2

7

Check out this Microsoft JDBC Blog post:

for resolve SSL problems with SQLServer (specially for android clients:JDBC driver can not be used effectively in the Android OS on unfortunately) try to using jDTS:

jTDS is an open source 100% pure Java (type 4) JDBC 3.0 driver for Microsoft SQL Server (6.5, 7, 2000, 2005, 2008 and 2012)

Sign up to request clarification or add additional context in comments.

2 Comments

You're right. It works for me. But anybody should keep in mind that JTDS 1.3.1 does not work with android so we must use 1.2.8
I'm successfully using jTDS 1.3.1 on Android, versions 5.0 through 9.0.
0

Is it

databaseName=MyDB

or

database=MyDB

?

Try the latter.

Are you on a Windows Machine? http://www.microsoft.com/en-us/download/details.aspx?id=24009

Download that and try to talk to your server outside of your code. The port query tool will show you if its a firewall (or similar) issue, outside of the code.

1 Comment

I downloaded the code from Microsoft page for Sqljdbc. I can connect to my server via Sql Server Management Studio and there is no problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.