Abdus Sami Khan
Abdus Sami Khan

Reputation: 277

The TCP/IP connection to host server.com/Db_name, port 1433 has failed

The question might be a basic one and if it is, pardon me. I'm trying to connect my Android app to MSSQL Server. I used the same code in my question in the link below. I have used sqljdbc 4.3.0 according to the chosen answer in this link:

com.mysql.jdbc.driver class not found exception

When I did this It debugs successfully but when I run it on my device which is Samsung Galaxy Y (Gingerbread) it gives me this error:

com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IP connection to the host server.com/Db_name, port 1433 has failed. Error: "null. Verify the connection properties, check that an instance of SQL Server is running on the host and accepting TCP/IP connections at the port, and that no firewall is blocking TCP connections to the port.".

Any help would be highly appreciated.

Edit: I ran it on the emulator and it gives this error in the text view.

android.os.NetworkOnMainThreadException

Upvotes: 2

Views: 6332

Answers (1)

Stephen C
Stephen C

Reputation: 719436

You got this exception:

com.microsoft.sqlserver.jdbc.SQLServerException:The connection to the 
          host server.com, named instance Db_name has failed. 
Error:"Java.net.SocketTimeoutException". Verify the server and the instance names, 
          check that no firewall is blocking UDP traffic to port 1434, and for 
          SQL Server 2005 or later verify that the SQL Server Browser Service 
          is running on the host.

Basically, you need to do what the exception message says:

  1. Check that you are using the correct server hostname. (Is it really "server.com"?)
  2. Check that you are using the correct instance name. (Is it really "Db_name"?)
  3. Check that there is no firewall blocking your access to UDP port 1434 from your device.
  4. Check that the SQL Server Browser Service is running on "server.com" (or whatever it really is).

A couple of other things to try on your device:

  • Check that you can do a DNS lookup of "server.com" (or whatever)
  • Check that you can "ping" the "server.com" host (or whatever)

Basically, there are a number of things that could be wrong, and you need to eliminate them methodically until you find the real cause of the problem. We can't do that for you.


I ran it on the emulator and it gives this error in the text view.

   android.os.NetworkOnMainThreadException

This is a different problem.

Here's what the javadoc says:

"The exception that is thrown when an application attempts to perform a networking operation on its main thread.

This is only thrown for applications targeting the Honeycomb SDK or higher. Applications targeting earlier SDK versions are allowed to do networking on their main event loop threads, but it's heavily discouraged. See the document Designing for Responsiveness."

In short, you shouldn't do network stuff (including talking to off-platform databases) on the main event thread ... 'cos it is likely to cause the UI to lock up for extended periods.

Upvotes: 1

Related Questions