Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I tried to connect to mysql database but I failed and this error is shown

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

}
like image 632
Mahmoud Elther Avatar asked Dec 03 '12 09:12

Mahmoud Elther


1 Answers

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

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.

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.1 with localhost and try to reconnect.

like image 200
cafebabe1991 Avatar answered Nov 10 '22 22:11

cafebabe1991