java - 与主机 server.com/Db_name、端口 1433 的 TCP/IP 连接失败

标签 java android sql sql-server jdbc

这个问题可能是一个基本问题,如果是,请原谅。我正在尝试将我的 Android 应用程序连接到 MSSQL Server。我在下面链接的问题中使用了相同的代码。根据此链接中选择的答案,我使用了 sqljdbc 4.3.0:

com.mysql.jdbc.driver class not found exception

当我这样做时它调试成功但是当我在我的三星 Galaxy Y( Gingerbread )设备上运行它时它给我这个错误:

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.".

任何帮助将不胜感激。

编辑:我在模拟器上运行它,它在 TextView 中给出了这个错误。

android.os.NetworkOnMainThreadException

最佳答案

你有这个异常:

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.

基本上,您需要按照异常消息中的说明进行操作:

  1. 检查您使用的服务器主机名是否正确。 (真的是“server.com”吗?)
  2. 检查您使用的实例名称是否正确。 (真的是“Db_name”吗?)
  3. 检查是否没有防火墙阻止您从设备访问 UDP 端口 1434。
  4. 检查 SQL Server 浏览器服务是否正在“server.com”(或任何实际名称)上运行。

在您的设备上尝试一些其他的事情:

  • 检查您是否可以对“server.com”(或其他)进行 DNS 查找
  • 检查您是否可以“ping”“server.com”主机(或其他主机)

基本上,有许多可能错误的地方,您需要有条不紊地消除它们,直到找到问题的真正原因。我们无法为您做到这一点。


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

   android.os.NetworkOnMainThreadException

这是一个不同的问题。

这是 javadoc 的内容说:

"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."

简而言之,您不应该在主事件线程上执行网络操作(包括与平台外数据库通信)...因为这可能会导致 UI 长时间锁定。

关于java - 与主机 server.com/Db_name、端口 1433 的 TCP/IP 连接失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15289083/

相关文章:

java - 发布阶段的Maven测试依赖

android - 如何在android中的textview上获取一个值作为htmltext?

android - 拦截调用以刷新 token GRPC

sql - 对多个表执行查询

sql - 在 WHERE 中使用 ROW_NUMBER() 别名

java - java中如何获取打印机列表?

Java时区: how to get UTC time values based on Console settings?

java - 可缓存注解有什么用

android - cordova apache 上的字体系列更改

mysql - sh 脚本在 crontab 中不起作用