【问题标题】:Communications link failure Last packet sent to the server was 1 ms ago.通信链路故障 发送到服务器的最后一个数据包是 1 毫秒前。
【发布时间】:2012-11-20 17:25:57
【问题描述】:

我尝试连接到mysql database,但失败并显示此错误

Communications link failure Last packet sent to the server was 1 ms ago

这是我的代码?任何人都可以帮助我,请

package android_programmers_guide.testconnection;


import java.sql.Connection;
import java.sql.DriverManager;

import android.os.Bundle;
import android.app.Activity;
import android.graphics.Color;
import android.view.Menu;
import android.widget.EditText;

public class TestConnection extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test_connection);



           Connection conn = null;
           String url = "jdbc:mysql://127.0.0.1:3306/test";
           String driver = "com.mysql.jdbc.Driver";
           String userName = "root"; 
           String password = "root";


           try {
           Class.forName(driver).newInstance();
           conn = DriverManager.getConnection(url,userName,password);


           EditText editText=(EditText) findViewById(R.id.editText1);


           editText.setBackgroundColor(Color.GREEN);
           editText.setText("Connected to the database");

           conn.close();

           editText.setBackgroundColor(Color.BLUE);
           editText.setText("Disconnected from database");


           } catch (Exception e) {
           e.printStackTrace();
           }



    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_test_connection, menu);
        return true;
    }

}

【问题讨论】:

  • 您确定您的 MySQL 数据库正在运行吗?或者也许 android_programmers_guide.testconnection 类正在尝试连接到 android 设备上的数据库
  • 我的数据库正在运行,抱歉我没看懂第二部分。我是 Java 初学者

标签: java android mysql mysqlconnection


【解决方案1】:

android 应用程序无法与同一系统中的mysql 通信。因为,当您运行 android 应用程序时,它会在 emulator 中运行。模拟器是一个virtual mobile device,就像一个安卓设备。 emulator 本身有一个 ip 地址。 因此,根据应用程序 ip 127.0.0.1 是模拟器的 ip,因此 android 应用程序将尝试与位于的 mysql 通信模拟器。众所周知,模拟器中不能有mysql,通信会失败。您可以将 SQLite 用于数据存储。 如果您希望您的android 应用程序与mysql 通信,您需要在另一个系统中安装mysql 并提供该系统的ip。修改你的代码如下:

String url = "jdbc:mysql://192.168.1.102:3306/test";

这里,我提到192.168.1.102 是第二个系统的ip。 在尝试之前不要忘记对这些系统进行物理连接。

更多知识:- mysql 与android 应用程序的直接通信不是正确的方式。 制作一个与 mysql 通信的 Web 应用程序。 在其中添加web service 以将数据存储到mysql。 在 android 应用程序中添加一个 web 层,通过该 web 服务与 web 应用程序通信。您可以使用 xml 或 JSON 将数据从 android 应用程序传输到 web通过 web 服务应用程序。web 应用程序应该将数据存储到 mysql 中,这些数据由 android 应用程序的 web 服务调用接收。

【讨论】:

    【解决方案2】:

    这个问题是因为你传递的 URL 不起作用。

    检查以下事项:

    1. 确保您的本地 MySQL 正在运行
    2. 通过输入\s 来检查 MySQL 的状态。
    3. 检查端口号并确保您使用的是同一端口号。

    如果以上都正确,可能是你没有权限。

    使用grant all on *.*通过“密码”识别,然后运行刷新权限,然后将127.0.0.1替换为localhost并尝试重新连接。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-09-02
      • 2022-01-07
      • 2012-11-20
      • 2011-08-19
      • 2016-04-01
      • 2015-01-18
      • 2020-01-13
      • 2013-01-11
      相关资源
      最近更新 更多