Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Cannot acces Mysql database

 
Nicolas Viollin
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
author & internet detective
Marshal
Posts: 34973
379
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nicolas,
Does it work if you try referencing server1 by IP address instead of name? Also, are you able to ping server1?
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic