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;
}
}
This problem is because the URL you are passing is not working.
Check the following things:
- Make sure that your localhost MySQL is running
- Check the status of MySQL by typing
\s
. - 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.
The android application cannot communicate with the mysql in the same system. Because, when you run the android application it runs inside the emulator. The emulator is a virtual mobile device like an android device. The emulator itself has an ip address.
So, according to the application the ip 127.0.0.1
is emulator's ip, therefore the android application will try to communicate with the mysql which is in the emulator. As we know, the emulator cannot have mysql in it, the communication will be failed. You can use SQLite for the data store.
If you want your android application to communicate with the mysql, you need install the mysql in another system and give that system's ip. Modify your code as follows :
String url = "jdbc:mysql://192.168.1.102:3306/test";
Here, I mentioned 192.168.1.102
is second system's ip.
And do not forget to make a physical connection of these systems before trying.
For an additional knowledge :- The direct communication of mysql with the android application is not a proper way. Make a web application which communicates with the mysql. Add a web service in 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 xml or JSON to 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.
来源:https://stackoverflow.com/questions/13679990/communications-link-failure-last-packet-sent-to-the-server-was-1-ms-ago