10

I have this Tetris game written in Java, which uses DB to record high scores. It worked ok as long as I was using remote MySQL DB, but now I'm trying to set up localhost DB using XAMPP MySQL and it keeps going like "SQLException: Communications link failure" at command:

con = java.sql.DriverManager.getConnection("jdbc:mysql://localhost/score", user, psw);

I guess it's either wrong URL or DB configuration, but I really don't know what to check. Any ideas?

EDIT: My friend has fixed my problem by replacing "localhost" in URL by "127.0.0.1" (which was quite embarrassing as you can surely imagine :P ).

So question is: Why is XAMPP not able to translate "localhost" into IP address and how to fix it?

4
  • This post claims to have fixed it. Check it out. Commented Jan 30, 2010 at 18:58
  • Mentioned skip-networking variable is commented in my XAMPP by default, so I afraid this is not the problem.. Commented Jan 31, 2010 at 0:58
  • Can you browse to the MySQL database with the Query Browser? I remember running into a similar issue with a Java app that refused to recognize naming in the connection string to MySQL. Commented Feb 3, 2010 at 20:57
  • Dont forget this important step on this link - ferdidolot.wordpress.com/2009/06/14/… Commented Nov 23, 2012 at 9:29

3 Answers 3

8

Why is XAMPP not able to translate "localhost" into IP address and how to fix it?

This is not a XAMPP problem nor a programming problem. This is more a DNS problem.

To start, do you have a %SystemRoot%/system32/drivers/etc/hosts file with the following line as first line? (thus, after all comments, but before any other host declarations)

127.0.0.1 localhost

Update: as per the comments I've Googled a bit and it look like that the MySQL JDBC driver doesn't eat IPv6 addresses at all. In other words, you'll need to change ::1 to 127.0.0.1. But I also found this topic which mentions that you can use the following JVM argument to fix this problem:

java -Djava.net.preferIPv4Stack=true 
Sign up to request clarification or add additional context in comments.

1 Comment

What about "::1 localhost"? That should do just the same, shouldn't it?
5

I tried and got a successful connection. First create a database in phpmyadmin - eg. 'mydb' and then in code put connection.url with this value

'jdbc:mysql://localhost:3306/mydb'

If you don't create a database first it wont connect

Comments

1

In MySql you have to allow access for your user from localhost explicitly. Here is an example (taken from here):

mysql> grant usage on *.* to amarokuser@localhost identified by 'amarokpasswd';
mysql> grant all privileges on amarokdb.* to amarokuser@localhost ;

1 Comment

I'm using root, but I've also tried to create user and give him access and all privileges, just to be sure... not working :/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.