1

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?

2
  • Your literally not connecting. Check your database url and make sure it's correct.
    – Sam Orozco
    Sep 25, 2016 at 19:19
  • I highly doubt a port is open at that address awaiting your connection or that it is authorized yet for the db
    – Drew
    Sep 25, 2016 at 19:19

2 Answers 2

1
  1. Check whether your MySQL database server is hosted/up/on.
  2. Check if the username and password entered of the connecting database is correct.
  3. Check if you have include the MySQL jar file in your build path and lib folder of your web server e.g. Tomcat
  4. Check the URL of the connecting database is correct.

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?

1
  • Looks like I need to find another host. They charge for remote access.
    – Queue
    Sep 25, 2016 at 19:54
1

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;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.