Friday, April 15, 2011

Published 4/15/2011 by with 0 comment

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


This error message may include the following text also:

"The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server."


When we try to connect to a MySQL server "Communications link failure" error may occur for any of the following errors:

i) MySQL server is not running. Check for this and run the sever if it is stopped.

ii) Host name is wrong: If the server is running in the same computer, the correct host name is localhost otherwise it is a valid IP address or computer name.

iii) Wrong port no given : By default MySQL server runs in port 3306. If it is different, check that correct port no is given.


Sample code:

public static Connection getConnection()
{
Connection connection = null;
try
{
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/database","username","password");
System.out.println("Connection successful");
}
catch(ClassNotFoundException cnfe)
{
System.err.println("MySQL driver not found");
cnfe.printStackTrace();
}
catch(SQLException sqle)
{
System.err.println("Database/Connection error");
sqle.printStackTrace();
}

return connection;
}



    email this       edit

0 comments:

Post a Comment