tags:

views:

61

answers:

3

Hi,

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

A: 

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.

Sean Owen
A: 

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...

Sebastien Lorber
A: 

Hey Sean, i am having same kind of problem, can u explain this port issue more deeply?

Also, Infiniti Fizz did you solve your problem? If so, can u tell me how did u do that?

Hi, no I didn't but it's because I didn't have admin rights over the database so it could well work otherwise. My solution was for the java to pass the variables I wanted updating to a php script which connected to the database and updated the data. Not too different to an AJAX style environment.
Infiniti Fizz