aspose file tools*
The moose likes JDBC and the fly likes java.net.ConnectException: Connection refused: connect Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "java.net.ConnectException: Connection refused: connect" Watch "java.net.ConnectException: Connection refused: connect" New topic
Author

java.net.ConnectException: Connection refused: connect

Ananth Chellathurai
Ranch Hand

Joined: Nov 21, 2007
Posts: 349

Hi Ranchers,

I have migrated to my vista laptop recently, And I am struggling to connect to Mysql from my Java application. The exception occurs only when I tried to connect to mysql in localhost, where as the same application can connect to mysql server running in other machines.

And I have made sure my user id and passwords are correct, I am able to connect with the same user id/password from command line and not from Java.

Please help me figure out this problem.

Regards
Ananth Chellathurai


Ananth Chellathurai [Walk on software]
Ananth Chellathurai
Ranch Hand

Joined: Nov 21, 2007
Posts: 349

The detailed exception when I connect to my MySQL5.0.67 server with mysql-connector-java-5.1.17 is as follows




and with 3.1.7 is

aleem khan
Ranch Hand

Joined: Aug 07, 2008
Posts: 94
I think you need to paste the complete code, then only we can figure out where exactly you are going wrong..but the possible solution could be
1)Change the port where your MYSQL listens, the default is 3306 so change it to something else(look for "conf" files where you can change port)

2)See if you can perform the same operation manually


SCJP(1.4), SCWCD, Oracle 9i SQL certified, Oracle PLSQL Developer Certified Associate
Ananth Chellathurai
Ranch Hand

Joined: Nov 21, 2007
Posts: 349

Thank you for your response. My problem is with only when I tried to connect from a Java application to a database in my local.

I am able to connect tomy local database through command line and through Query browser and the port I used is 3306.

And my code I used to connect to database is a working code, And the same code is able to connect with databases in other machines.

Is it something to do with the environment? Should I have to install 64bit mysql?

Any help on this is greatly appreciated.

Ananth Chellathurai
Ananth Chellathurai
Ranch Hand

Joined: Nov 21, 2007
Posts: 349

I am yet to figure out the issue. I am still not able to connect to my MySQL when I give the host name as localhost. Luckily I had a workaround to give the machine name instead of localhost to connect to MySQL from java.

Hope this helps.

Regards
Ananth Chellathurai
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

Ananth Chellathurai wrote: Luckily I had a workaround to give the machine name instead of localhost to connect to MySQL from java.

Hope this helps.

Is it a solution you found OR you're asking us ?
And giving machine name, solves your problem, then try giving its IP address, If its local then you know the IP address, 127.0.0.1

And this lines cause the problems,

So providing the code snippet for the same, will be appreciated


[LEARNING bLOG] | [Freelance Web Designer] | [and "Rohan" is part of my surname]
Deepa sunder
Greenhorn

Joined: Mar 26, 2008
Posts: 2
Hi Ananth,

I'm also facing the same problem in my Vista. I'm able to connect through the Query Browser but when i try to connect from my java code i get this exception.
Did you find any solution to this problem. Please help me out.. I'm stuck with this issue.

Ananth Chellathurai
Ranch Hand

Joined: Nov 21, 2007
Posts: 349

Instead of localhost as your host name, try with your machine name.

Ananth Chellathurai
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Deepa sunder wrote:Hi Ananth,

I'm also facing the same problem in my Vista. I'm able to connect through the Query Browser but when i try to connect from my java code i get this exception.
Did you find any solution to this problem. Please help me out.. I'm stuck with this issue.



Vista comes with a firewall. Have you check this is not what is blocking the connection?


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Deepa sunder
Greenhorn

Joined: Mar 26, 2008
Posts: 2
Thanks Ananth.

Fixed the issue. I replaced the localhost with the ip address i,e 127.0.0.1. It started working fine.
I realized that there wasnt any mapping in the host file of vista. Later i mapped the localhost to ip address it started working without any changes in the code.

PS: Vista host file must be changed using the administrative privilege otherwise the changes doesn't reflect.

Ananth Chellathurai
Ranch Hand

Joined: Nov 21, 2007
Posts: 349

Thank you deepa for the tip.

Ananth Chellathurai
[Ananth's walk on software, Gotchas Rocks]
Gert Cuppens
Ranch Hand

Joined: Jul 13, 2003
Posts: 87
I had the same problem during the last 3 weeks. And since I can only program in Java after my working hours, a week only means about 18 hours to look for the solution. I have searched several forums for the solution but thanks to Joeri Leemans, a Java pro, I looked at this thread.
And changing localhost into 127.0.0.1 was also for me the great breakthrough.
Thanks, guys !
Liam Johnstone
Greenhorn

Joined: Oct 08, 2009
Posts: 2
Ananth, Deepa, and Gert:

Thank you, thank you, thank you.

I don't know how you found that out, the difference between localhost and 127.0.0.1, but I've been looking for the answer to this for three or four weeks.

Totally changes my outlook on the day.

Thank you again,

Liam
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: java.net.ConnectException: Connection refused: connect
 
Similar Threads
JPA/Hibernate: Selecting data from second database?
Java MySQL connection
"main" java.sql.SQLException: Access denied for user 'root'@'192.168.1.1' (using password: YES)
Struts with MySql
connection of eclipse helios and mysql