File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JDBC and the fly likes Cannot acces Mysql database Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Cannot acces Mysql database" Watch "Cannot acces Mysql database" New topic
Author

Cannot acces Mysql database

Nicolas Viollin
Greenhorn

Joined: Apr 13, 2001
Posts: 26
Hello,

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: java.net.ConnectException: Connection refused: connect
at com.mysql.jdbc.Connection.createNewIO(Connection.java:1719)
at com.mysql.jdbc.Connection.<init>(Connection.java:432)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:400)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at ClientMysql.<init>(ClientMysql.java:24)
at ClientMysql.main(ClientMysql.java:33)

Here is my code:

try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
java.sql.Connection conn;

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

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
internet detective
Marshal

Joined: May 26, 2003
Posts: 30068
    
149

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


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
David O'Meara
Rancher

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!
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Cannot acces Mysql database
 
Similar Threads
Hibernate Dialect for MS Access
How to tell which port mySQL is running on?
java.policy setting not reflecting
client cant connect to mysql
Connect to MySql with JDBC