Reputation: 456
I am running a database through 000WebHosting.com. When I try to connect to my database, I get the following error:
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.
This is my code:
String url = "jdbc:mysql://" + "mysql2.000webhost.com/";
try
{
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection conn = DriverManager.getConnection(url + "DB_NAME", userName, passWord);
}
catch (InstantiationException | IllegalAccessException | SQLException e)
{
System.out.println(e);
}
And the exception continues to arise on the conn
line.
What am I doing wrong?
Upvotes: 1
Views: 2320
Reputation: 596
Also do this changes and see
String url = "jdbc:mysql://mysql2.000webhost.com/";
Also I found this
MySQL from 000webhost doesn't allow you to connect from external applications, just from within pages hosted in their domain.
Please check: How can I connect to MySQL from my computer?
Upvotes: 1
Reputation: 91
Try to initilize your db connection like this (put your db name in the URL):
final static String HOSTNAME = "yourdomain.com";
final static String PORT = "3306";
final static String USER = "youruser";
final static String PWD = "yourpassword";
final static String DBNAME = "dbname";
public java.sql.Connection setupConnection() {
java.sql.Connection conn = null;
try {
Class.forName("com.mysql.cj.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
System.out.println("Connection is being established...");
String url = "jdbc:mysql://" + HOSTNAME + ":" + PORT + "/" + DBNAME +
"?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC";
try {
conn = DriverManager.getConnection(url, USER, PWD);
} catch (SQLException e) {
e.printStackTrace();
}
System.out.println("Database connection successfully established!");
return conn;
}
Upvotes: 1