java 通信链路故障 最后一个发送到服务器的数据包是 1 毫秒前。

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13679990/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 13:35:06  来源:igfitidea点击:

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

javaandroidmysqlmysqlconnection

提问by Mahmoud Elther

I tried to connect to mysql databasebut I failed and this error is shown

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

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

and this is my code ? anyone can help me please

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

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;
    }

}

回答by cafebabe1991

This problem is because the URL you are passing is not working.

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

Check the following things:

检查以下事项:

  1. Make sure that your localhost MySQL is running
  2. Check the status of MySQL by typing \s.
  3. Check the port number and make sure you are using the same one.
  1. 确保您的 localhost MySQL 正在运行
  2. 通过键入检查 MySQL 的状态\s
  3. 检查端口号并确保您使用的是相同的端口号。

If all of the above is correct, maybe you don't have permissions.

如果以上所有内容都正确,则可能是您没有权限。

Use grant all on *.*to identified by "password",then run flush privileges, and then replace 127.0.0.1with localhostand try to reconnect.

使用grant all on *.*通过“密码”标识,然后运行flush privileges的,然后替换127.0.0.1localhost,并尝试重新连接。

回答by Visruth

The android application cannot communicate with the mysqlin the same system. Because, when you run the android application it runs inside the emulator. The emulator is a virtual mobile devicelike an android device. The emulatoritself has an ipaddress. So, according to the application the ip127.0.0.1is emulator's ip, therefore the android application will try to communicate with the mysqlwhich is in the emulator. As we know, the emulator cannot have mysqlin it, the communication will be failed. You can use SQLitefor the data store. If you want your android application to communicate with the mysql, you need install the mysqlin another system and give that system's ip. Modify your code as follows :

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

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

Here, I mentioned 192.168.1.102is second system's ip. And do not forget to make a physical connection of these systems before trying.

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

For an additional knowledge :- The direct communication of mysqlwith the android application is not a proper way. Make a web application which communicates with the mysql. Add a web servicein it to store data into mysql. In the android application add a web layer which communicates with the web application through this web service.You can use xmlor JSONto transfer data from the android application to web application via web service.The web application should be storing data in to mysql which are received by the web service call of the android application.

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