понедельник, 1 ноября 2010 г.

MySQL: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

Recently I got next exception:
com.mysql.jdbc.exceptions.jdbc4.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.

I looking into google, but got no answer, except for "check your server", "check port of your server", "check localhost at /etc/hosts" and some other unavailing stuff.
Problem was in my code, I feel, that I'm Great Indian Coder, then detect it.
Connection conn = DriverManager.getConnection("jdbc:mysql://" + 
app.sql.settings.constants.getHost() +
app.sql.settings.constants.getPort() +
app.sql.settings.constants.getDb() +
app.sql.settings.constants.getUser() +
app.sql.settings.constants.getPassword());

I wrote it too fast and I was distracted on something and don't noticed it. Correct code is:
Connection conn = DriverManager.getConnection("jdbc:mysql://" + 
app.sql.settings.constants.getHost() + ":" +
app.sql.settings.constants.getPort() + "/" +
app.sql.settings.constants.getDb(),
app.sql.settings.constants.getUser(),
app.sql.settings.constants.getPassword());

because correct format of getConnection's parameters is:
jdbc:mysql://host:port/db, user, password

I will be glad if it will help you.

Комментариев нет:

Отправить комментарий