File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JDBC and Relational Databases and the fly likes Cannot acces Mysql database Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "Cannot acces Mysql database" Watch "Cannot acces Mysql database" New topic

Cannot acces Mysql database

Nicolas Viollin

Joined: Apr 13, 2001
Posts: 26

I am writing a program to access a Mysql database. the database is located on a dedicated server (say server1). When i run my program from that pc (server1), setting the address to "localhost", everything is fine. but if i try to run the program from an other pc (say pc1) and set the address to server1, then is fails and i've got the following message:

java.sql.SQLException: Unable to connect to any hosts due to exception: Connection refused: connect
at com.mysql.jdbc.Connection.createNewIO(
at com.mysql.jdbc.Connection.<init>(
at com.mysql.jdbc.NonRegisteringDriver.connect(
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at ClientMysql.<init>(
at ClientMysql.main(

Here is my code:

try {
java.sql.Connection conn;

conn = DriverManager.getConnection("jdbc:mysql://server1:3306/albumphotos?username=username&password=passsword");
System.out.println("Connected !");
}catch (Exception ex) {

those two pcs are on the same LAN, there is no access restriction between the two pcs set on the firewall. the username for the database is set to any server (i.e not only localhost).

I tried on an other pc without more success :-(

please help !!
Jeanne Boyarsky
author & internet detective

Joined: May 26, 2003
Posts: 33124

Does it work if you try referencing server1 by IP address instead of name? Also, are you able to ping server1?

[OCA 8 book] [Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
David O'Meara

Joined: Mar 06, 2001
Posts: 13459

my first step when setting up jdbc is to use SquirrelSQL to connect to the database, and then write the code. This allows you to manage database setup and configuration problems withoiut worrying about code concerns.
I think Jeanne's right though, it looks like there's something going on here which is failing before anything JDBC-related. Either database is not running, or is on a non-standard port, or the machine name is wrong or mapped to the wrong IP, or there is a firewall issue. Good luck though!
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
subject: Cannot acces Mysql database
It's not a secret anymore!