0

I'm trying to connect to my database with a singelton class in java. This is my current code:

public class Database {

public static Database instance;

private Connection connection;

private Database() {
    try {
        Class.forName("com.mysql.jdbc.Driver");
        Log.debug("ok");
        this.connection = DriverManager.getConnection("jdbc:mysql://just.not.for.you:3006/database?connectTimeout=5000", "database", "secret");
        Log.debug("ok");
    } catch (ClassNotFoundException e) {
        Log.warn("Cloud not find jdbc driver class");
    } catch (SQLException e) {
        Log.warn("Cloud not connect to database: " + e.getMessage(), e);
    }
}


public static synchronized Database getInstance() {
    if(Database.instance == null) {
        Database.instance = new Database();
    }
    return Database.instance;
}

In my main class i call Database.getInstance() but nothing happends. No error, no timeout message. It seems like an endless loop. I see the first debug message in line 10 but not the message in line 12.

What is the problem?

Edit: I can connect to the remote database with my local computer with mysql workbench

3
  • Have you tried setting system property jdbc.drivers to com.mysql.jdbc.Driver or creating file META-INF/services/java.sql.Driver with driver's class name as its content? Commented Jun 23, 2018 at 18:06
  • 1
    Is the port number correct? You're attempting to connect to port 3006 but port 3306 is the usual port for MySQL. Commented Jun 23, 2018 at 18:16
  • You are right. The port was the problem. Facepalm _ Commented Jun 23, 2018 at 18:18

1 Answer 1

1

I found some problem in your code!

  • Replace com.mysql.jdbc.Driver with new drivercom.mysql.cj.jdbc.Driver, because it is deprecated
  • Checkout mysql server port, default port is 3306 not 3006
Sign up to request clarification or add additional context in comments.

1 Comment

The newest driver still contains com.mysql.jdbc.Driver for backwards compatibility.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.