开发者

Connecting to hosted MySQL server with Java

开发者 https://www.devze.com 2022-12-28 02:45 出处:网络
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:

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 somethin开发者_运维技巧g wrong?

Thanks for your time,

InfinitiFizz


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消