Is there a MySQL server running on the machine/port you are trying to connect to?
Dmitry Petrov
Greenhorn
Joined: Feb 27, 2010
Posts: 2
posted
0
Hello!
I relatively new to Java, and am currently working on a project, for which I would like to be able to connect to an online mySQL database. One that's being hosted somewhere ... else.
For days, I have been trying to figure out why it won't work. I have tried countless tutorials on J/Connector, and they all come up with the same error;
Is there a MySQL server running on the machine/port you are trying to connect to?
In my last attempt, I have followed this tutorial, word for word.
Connecting to localhost failed, as did to GoDaddy, and FreeMySql.net, and even a public genome database.
The people over at the Java forums didn't help at all, so I come here seeking dire assistance.
Not sure what code to post, as I have tried so much, and it all comes down to the same error.
Are you able to use the mysql command line tools to connect to the remote database?
Also, since you are just now starting out with JDBC, I would recommend that your install MySQL locally and work with that. Most tutorials assume that you will have a local MySQL database and thus use 'localhost' in the URL. Once you are comfortable with coding for your local database you can easily substitute the URL for for local database with the one for the remote database.
MySQL Driver Found
Connection couldn't be established to jdbc:mysql://localhost:3306/feedback
Exception in thread "main" java.sql.SQLException: Cannot connect to MySQL server on localhost:3306. Is there a MySQL server running on the machine/port you are trying to connect to? (java.lang.NumberFormatException)
at com.mysql.jdbc.Connection.connectionInit(Unknown Source)
at com.mysql.jdbc.jdbc2.Connection.connectionInit(Unknown Source)
at com.mysql.jdbc.Driver.connect(Unknown Source)
at java.sql.DriverManager.getConnection(DriverManager.java:582)
at java.sql.DriverManager.getConnection(DriverManager.java:185)
at dbCon.makeCon(dbCon.java:24)
at dbCon.runMe(dbCon.java:10)
at dbCon.main(dbCon.java:63)
Process completed.
Well, I use Navicat to connect to mySQL servers, and they work fine.
As for localhost, yeah, I have tried that several times, unfortunately to no avail.
Nirmal A Janardanan
Greenhorn
Joined: Feb 28, 2010
Posts: 1
posted
0
From the stack trace, it seems that the MySQL server is not running at localhost:3306
Connection couldn't be established to jdbc:mysql://localhost:3306/feedback
Exception in thread "main" java.sql.SQLException: Cannot connect to MySQL server on localhost:3306. Is there a MySQL server running on the machine/port you are trying to connect to? You can do a telnet and check if MySQL is up or dead. Open your command prompt/linux console and run this cmd.
telnet localhost 3306 If it says.. "Connection refused or something similar", then MySQL service is not started. To start mysql on linux you can run
/etc/init.d/mysql start If connection was successful, then check if you have created a database 'feedback' in mysql which you are connecting in your code.
Hope this helps
The problem is that you are using "localhost" in the connection URL. As you stated in your original post you are using a remote mysql server. You need to find out from that mysql provider what URL you should be using. If they don't know the URL, ask them for the host name and port number. I assume that you know the database. From that information you can build the connection URL.
The alternative is to follow my suggestion and install MySQL locally. I highly recommend that!