Infiniti Fizz
Infiniti Fizz

Reputation: 1726

Connecting to hosted MySQL server with Java

I've been recently trying to connect to a hosted MySQL using Java but can't get it to work. I can connect to a local MySQL with localhost using:

connect = DriverManager.getConnection("jdbc:mysql://localhost/lego?"
                + "user=******&password=*******");

(Replacing the astrisks withmy username and password)

I can connect to the hosted MySQL database fine with PHP using:

mysql_connect('mysql.hosts.co.uk','******','**********');
mysql_select_db('test');

My problem is, I cannot connect via Java. I have an Exception which is caught if the connection doesn't work and this is always printed out.

Any ideas why it isn't working? Am I doing something wrong?

Thanks for your time,

InfinitiFizz

Upvotes: 1

Views: 657

Answers (2)

Sebastien Lorber
Sebastien Lorber

Reputation: 92130

since it works in php (i guess you didn't try to connect from a local place with php???) it shouldn't be a port problem... but you should check that port 3306 is open... and ask the hosts company about that.

Have you noticed that in the DriverManager http://java.sun.com/javase/6/docs/api/java/sql/DriverManager.html you have: getConnection(String url) but also: getConnection(String url, String user, String password)

Perhaps it would work better...

Upvotes: 1

Sean Owen
Sean Owen

Reputation: 66886

My guess is that you need to select a non-standard port, since I'd imagine the hosting server is serving lots of MySQL instances and they can't all use the normal one. I don't see selection of a port here.

If that's not it, perhaps there is a firewall issue somewhere along the way that's blocking the port or connection.

Upvotes: 0

Related Questions