Reputation: 27
When I try to connect my java program to the server I get this exception:
CommunicationsException: Communications link failure
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
So I found these instructions:
If you get a SQLException: Connection refused or Connection timed out or a MySQL specific CommunicationsException: Communications link failure, then it means that the DB isn't reachable at all. This can have one or more of the following causes:
- IP address or hostname in JDBC URL is wrong.
- Hostname in JDBC URL is not recognized by local DNS server.
- Port number is missing or wrong in JDBC URL.
- DB server is down.
- DB server doesn't accept TCP/IP connections.
- DB server has run out of connections.
- Something in between Java and DB is blocking connections, e.g. a firewall or proxy.
To solve the one or the other, follow the following advices:
- Verify and test them with ping.
- Refresh DNS or use IP address in JDBC URL instead.
- Verify it based on my.cnf of MySQL DB.
- Start the DB.
- Verify if mysqld is started without the --skip-networking option.
- Restart the DB and fix your code accordingly that it closes connections in finally.
- Disable firewall and/or configure firewall/proxy to allow/forward the port.
from this answere.
I tried every option but im not sure how to verify the port number (option 3) with 'my.ini' configuration file. Plus, I found that the service of 'MySQL' is missing in the services.msc list, but I did find the port 3306 is used. Is it related to the issue? And how 'my.ini' configuration file should look like? Im using MySQL Workbench 5.2.47. (MySQL server version is 5.6).
my connection code:
private MySQLConnectivity() {
try { Class.forName("com.mysql.jdbc.Driver"); Connect();
} catch (ClassNotFoundException e)
{ // TODO Auto-generated catch block e.printStackTrace(); }
}
public void Connect() { try { MySQLConnectivity.Conn = (Connection)DriverManager.getConnection(URL, USER, PASS);
} catch (SQLException e)
{ // TODO Auto-generated catch block e.printStackTrace();
} finally { }
Upvotes: 0
Views: 3054
Reputation: 31
I use the latest version of adt and mysql-connector-java-5.1.17-bin.jar
, this work for me :
Add the below code into your mafile after setContentView(R.layout.activity_main)
;
if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}
and use import android.os.StrictMode;
If you use new api, add @SuppressLint("NewApi");
Upvotes: 2