【问题标题】:Communications link failure while connecting to mysql连接 mysql 时通信链接失败
【发布时间】:2012-12-21 05:38:22
【问题描述】:

我正在尝试从我的 android 应用程序连接到 mysql 数据库。

我收到Communications link failure错误。
下面是代码sn-p:

public class MySqlConnector {
    private Connection con = null;
    private String s = "";
    private String username = "root";
    private String password = "password01";
    private String connectionString;

    public String ConnectToDb() {
         connectionString ="jdbc:mysql://192.168.1.104:3306/mydatabase";
        //connectionString="jdbc:mysql://10.0.0.0:3306/mydatabase";
        // connectionString="jdbc:mysql://127.0.0.1:3306/mydatabase";
        //connectionString = "jdbc:mysql://MainSrv04:3306/mydatabase";
        // connectionString="jdbc:mysql://localhost:3306/mydatabase";

        // connectionString =
        // "jdbc:mysql://localhost:3306/mydatabase?user=root&password=password01&useUnicode=true&characterEncoding=UTF-8";

        try {
            Class.forName("com.mysql.jdbc.Driver").newInstance();

        } catch (Exception ex) {
            ex.printStackTrace();
        }
        try {
            con = DriverManager.getConnection(connectionString, username,
                    password);

            Statement st = con.createStatement();
            String sql = "SELECT First_Name FROM mydatabase.custinfo where CardNumber=5325784707";
            ResultSet rs = st.executeQuery(sql);
            s = rs.getString("First_Name");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (con != null) {
                try {
                    con.close();
                    Log.i("MySqlConnector", "Database connection terminated");
                } catch (Exception e) { /* ignore close errors */
                }
            }
        }

        if (s == "") {
            s = "No Result";
        }
        Log.i("MySqlConnector : s=", s);

        return s;
    }
}

我尝试了所有可能的组合,如 cmets 中所示,并在 由 logcat 中的字段引起以下错误:

  • 使用本地主机时 --> Caused by: java.net.ConnectException: localhost/127.0.0.1:3306 - Connection refused
  • 在使用 127.0.0.1 时 --> Caused by: java.net.ConnectException: /127.0.0.1:3306 - Connection refused
  • 在使用 10.0.0.0 时 --> Caused by: java.net.SocketException: The operation timed out
  • 在使用 192.168.1.104 时 --> Caused by: java.net.SocketException: The operation timed out
  • 使用 MainSrv04 时 --> Caused by: java.net.UnknownHostException: MainSrv04

我还通过 telnet ping mysql 端口,它正在工作。
另外,我已经处理了特权。

但我仍然收到Communications link failure 错误。

任何帮助表示赞赏。

【问题讨论】:

  • 那么你的 MySQL 在哪个主机/端口是? Android 设备是否在同一网络上?
  • 3306。我在 connectionString 变量中提到过。
  • 如果你使用这个jdbc:mysql://localhost:3306/mydatabase?user=root&password=password01会发生什么?
  • 你看过这个链接吗? MySQL and Java JDBC - Tutorial
  • 你的 MySQL 服务器允许远程连接吗?

标签: java android mysql


【解决方案1】:

android 应用程序是否在同一个网络上?如果没有

jdbc:mysql://192.168.1.104:3306

和

jdbc:mysql://10.0.0.0:3306

不会工作。

给出正确的mysql网络地址。

【讨论】:

  • 请看问题。我尝试了 5 种不同的组合来指定网络地址和相应的问题。
  • 如果应用程序在同一个地址,那么 192.168.104 方法应该可以正常工作。在这种情况下你应该看看这篇文章stackoverflow.com/questions/585599/…
  • 如果我使用192.168.1.104:3306,我会得到Caused by: java.net.SocketException: The operation timed out....
  • connectionString = "jdbc:mysql://192.168.1.104:3306/dbname?user=root&password=password01"; 工作...
【解决方案2】:

我使用 最新版本的 adt 和 mysql-connector-java-5.1.17-bin.jar,这对我有用:

在setContentView(R.layout.activity_main);行之后将以下代码添加到您的文件中

if (android.os.Build.VERSION.SDK_INT > 9) {
    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
    StrictMode.setThreadPolicy(policy);
}

并使用import android.os.StrictMode;

如果你使用新的api,请添加@SuppressLint("NewApi");

【讨论】:

    猜你喜欢
    • 2015-01-27
    • 1970-01-01
    • 1970-01-01
    • 2021-01-12
    • 2017-07-31
    • 2019-03-01
    • 1970-01-01
    • 2014-06-09
    • 2016-09-25
    相关资源
    最近更新 更多