Mike
Mike

Reputation: 1312

Can't connect to godaddy MySQL with java

I'm trying to connect to a mysql db, but I keep getting error: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

I know the url suppose to be: "jdbc:mysql://server-url:3306"

here is the code:

import java.sql.Connection;
import java.sql.DriverManager;

public class FT_Database_Connection{
private Connection connection;

public FT_Database_Connection(String url, String username, String password){
    try{
        System.out.println("Loading driver...");
        Class.forName("com.mysql.jdbc.Driver");
        System.out.println("Driver loaded!");

        String user = username;
        String db_pw = password;
        System.out.println(url);
        System.out.println(username);
        System.out.println(password);
        Connection test = DriverManager.getConnection(url, user, db_pw);

    }catch(Exception e){
        System.out.println("Couldn't get database connection.");
        e.printStackTrace();
    }
}

public Connection get_connection(){
    return this.connection;
}

}

Any ideas?

Also, Godaddy ask if I want DSN. What's that mean? Do I need it?

Thanks

Upvotes: 1

Views: 3610

Answers (3)

eric
eric

Reputation: 170

I hate to add a new post to something so old but I ran across the same issues with GoDaddy and their dbname.db.######.hostedresource.com implementation. Granted, I looked across a few Overflow posts and saw no mention of the direct IP method; hopefully I'm not breaking some GoDaddy code here.

Any who, the only way I was able to connect through my Workbench & through JDBC was by going to the PHP admin page and grabbing the direct IP to the database - in my case :

private static final String URL = "jdbc:mysql://11.11.111.11:3306/dbname";

Upvotes: 0

Eric B.
Eric B.

Reputation: 24441

If memory serves, GoDaddy has a couple of different MySQL DB implementations you can choose. One that allows for remote access, and one that does not. Are you sure that you have chosen the correct one?

Have you tried connecting remotely using MySQL Workbench first? Or even telnetting to the port? That will ensure you have the right db name and permissions to connect remotely. Once you can confirm that works, getting the code to work should be easier.

Upvotes: 1

sfell77
sfell77

Reputation: 986

You need to put your database name after the MySQL port (:3306/DB_name). Here's one of my connections:

import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.SQLException;

public class MySQL{

    static Connection connect;
    public static void main(String[] args) throws InstantiationException, IllegalAccessException, ClassNotFoundException, SQLException {
    Credentials();

    Class.forName ("com.mysql.jdbc.Driver").newInstance ();

    String url = "jdbc:mysql://" + host + ":3306/" + datab;

    Driver test = DriverManager.getDriver(url);
    // testing to make sure i'm using the correct
    //driver and further testing host
    System.out.println(test);
    connect = DriverManager.getConnection(url, userName, password);

    System.out.println ("Connected to " + host);
    }
}

You'll need to supply the "host" and "datab" string values

Upvotes: 2

Related Questions