Communication Communication Error The last packet sent to the server was 1 ms ago.

I tried connecting 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 is this my code? anyone can help me.

 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; } } 
+8
java android mysql mysqlconnection
source share
2 answers

This problem is because the url you submit is not working.

Check out the following things:

  • Make sure your localhost MySQL is running
  • Check the status of MySQL by typing \s .
  • Check the port number and make sure that you are using it.

If all of the above is correct, you may not have rights.

Use grant all on *.* To authenticate with a "password", then grant all on *.* flush privileges, then replace 127.0.0.1 with localhost and try connecting again.

+1
source share

The android application cannot communicate with mysql on the same system. Because when you launch an Android app, it runs inside the emulator. An emulator is a virtual mobile device , such as an Android device. The emulator itself has an IP address. Thus, according to the application ip 127.0.0.1 is an emulator ip, so the android application will try to establish a connection with mysql, which is located in the emulator. As you know, the emulator cannot have mysql in it, the connection will fail. You can use SQLite for data storage. If you want your Android application to communicate with mysql, you need to install mysql on another system and provide this ip system. Change the code as follows:

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

Here I mentioned 192.168.1.102 - the second ip system. And do not forget to physically connect these systems before you try.

For additional knowledge: - Direct connection of mysql with the android application is not the right way. Create a web application that communicates with mysql. Add a web service to store data in mysql. In an android app, add a web tier that communicates with the web app through this web service. You can use xml or JSON to transfer data from an Android application to a web application through a web service. The web application must store data in mysql, which is accepted by calling the web service of the Android application.

0
source share

All Articles