android 中Network error IOException: failed to connect to /127.0.0.1 (port 1433): connect failed: ECONNREFUSED (Connection refused)

下面对android中出现的Network error IOException: failed to connect to /127.0.0.1 (port 1433): 做一下总结:

当用android程序调用本地的sqlserver的时候,当连接ip写成127.0.0.1的时候,怎么都连不通,出现以下的错误信息:

感觉好像是代码有问题或者是清单文件中某个权限忘加了,其实不是这样的。原来模拟器默认把127.0.0.1和localhost当做本身了,在模拟器上可以用10.0.2.2代替127.0.0.1和localhost,另外如果是在局域网环境可以用 192.168.0.x或者192.168.1.x(根据具体配置)连接本机,这样应该就不会报错了。

package com.example.zhouy.mytest01;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * Created by Administrator on 2017/12/14.
 */

public class DBUtil {
    private static Connection getSQLConnection(String ip,String user,String pwd,String db)
    {
        Connection con =null;
        try {
            Class.forName("net.sourceforge.jtds.jdbc.Driver");
            con = DriverManager.getConnection("jdbc:jtds:sqlserver://"+ip+":1433/"+db+";charset=utf-8",user,pwd);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return con;

    }

    public static String QuerySQL(){

        String result="";

        try {
            Connection conn = getSQLConnection("192.168.1.101","yonghuming","mima","SHUJUKU");
            String sql = "select top 10 * from users";
            Statement stmt = conn.createStatement();
            ResultSet rs = stmt.executeQuery(sql);
            while(rs.next())
            {
                String s1 = rs.getString("USRNAM");
                String s2 = rs.getString("FULLNAME");
                result += s1+" - " + s2 + "\n";
                System.out.println(s1+" - "+s2);
            }
            rs.close();
            stmt.close();
            conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
            result += "查询数据异常!"+e.getMessage();
        }
        return result;
    }

    public static void main(String[] args){
        QuerySQL();

    }
}
时间: 2024-08-16 15:52:48

android 中Network error IOException: failed to connect to /127.0.0.1 (port 1433): connect failed: ECONNREFUSED (Connection refused)的相关文章

Android客户端连接tomcat时出错:connect failed: ECONNREFUSED (Connection refused)

public static final String BASE_URL = "http://127.0.0.1:8090/HelloWord/"...; 会抛异常Caused by: libcore.io.ErrnoException: connect failed: ECONNREFUSED (Connection refused) 解决方法: String url = "http://127.0.0.1:8090/HelloWord/"...;  修改成 pub

java.net.ConnectException: failed to connect to /10.0.2.2 (port 80): connect

在使用GENYMOTION作为Android程序调试模拟器连接web服务器时,报了:java.net.ConnectException: failed to connect to /10.0.2.2 (port 80): connect 解决方法:使用genymotion进行调试时,不能使用10.0.2.2进行调试,而必须使用电脑主机的ip地址,类似于模拟器是通过wifi连接到主机. 使用ipconfig获得ip地址,在使用IP地址连接即可.

王立平--android中让程序终止退出的解决办法 System.exit(0)

System.exit(0); System.exit(0)是将你的整个虚拟机里的内容都停掉了, 而finish()只是退出了activity,并没有退出应用,Application还是存在于内存中的,除非被系统回收. System.exit(0)是正常退出程序, System.exit(1)或者说非0表示非正常退出程序 System.exit(status)不管status为何值都会退出程序. 和return 相比有以下不同点:return是回到上一层,而System.exit(status)

android中出现Error retrieving parent for item: No resource found that matches the Theme.AppCompat.Light

styles.xml中<style name="AppBaseTheme" parent="Theme.AppCompat.Light">提示如下错误,这是版本问题. error: Error retrieving parent for item: No resource found that matches the given name 'Theme.AppCompat.Light'. 解决方法: 在Eclipse中打开任意一个.java文件,输入  

java.net.ConnectException: failed to connect to /127.0.1.1 (port 8080)的解决办法

当你启动Tomcat后,在浏览器中输入http://127.0.1.1:8080/xxx.xx,你会发现你可以找打你要找的东西.但是当你在Eclipse中运行测试代码后,JUnit会一直报出这样的错误: java.net.ConnectException: failed to connect to /127.0.1.1 (port 8080) after 5000ms: isConnected failed: ECONNREFUSED (Connection refused)at libcore

android模拟器访问localhost或127.0.0.1报错

在一般的Java Web程序开发中,我们通常使用localhost或者127.0.0.1来访问本机的Web服务,但是如果我们在Android模拟器中也采用同样的地址来访问,Android模拟器将无法正常访问到我们的服务,这是为什么呢?我们可以这样来理解:Android的底层是Linux kernel,包括Android本身就是一个操作系统,因此,这时我们在模拟器的浏览器中输入的localhost或127.0.0.1所代表的是Android模拟器(Android虚拟机),而不是你的电脑,明白了吗?

mariadb:InnoDB: Error: log file ./ib_logfile0 is of different size 0 5242880 bytes

mariadb 启动中 InnoDB: Error: log file ./ib_logfile0 is of different size 0 起因:线上正在运行的系统,因为需要调整性能,变更了my.cnf参数的innodb_log_file_size大小,重启MySQL时err日志输出如下InnoDB: Error: log file ./ib_logfile0 is of different size 0 xxxx bytes 停掉mariadb 解决办法:移除原有ib_logfile#m

Android中使用databinding编译时出现的error:Execution failed for task &#39;:app:dataBindingProcessLayoutsDebug&#39;

Windows环境下使用svn对AndroidStudio更新代码时,总会在源文件中出现一堆乱码,尤其是xml文件中的乱码,不仅找起来费劲,改起来更费劲. 最近从svn更新代码之后,编译时出现了下面这个提示,而且AS中没有错误提示,这可真是捉急了. databinding error:Execution failed for task ':app:dataBindingProcessLayoutsDebug' 后来,多亏这篇帖子提示,自己写了段代码来尝试下,http://stackoverflo

Android中连接蓝牙设备时遇到createRfcommSocketToServiceRecord的UUID问题和BluetoothSocket的connect失败

[问题] 折腾: [记录]编写Android中的蓝牙模块驱动和底层HART设备 期间,参考: Bluetooth | Android Developers – ManagingAConnection 参考“Connecting as a client”中的: tmp = device.createRfcommSocketToServiceRecord(MY_UUID); 遇到UUID不懂的问题. 然后随便去 http://www.guidgenerator.com/online-guid-gen