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:
我有这个用Java编写的俄罗斯方块游戏,它使用DB来记录高分。只要我使用远程MySQL数据库就可以正常工作,但现在我正在尝试使用XAMPP MySQL设置localhost数据库,并且它在命令中继续像“SQLException:Communications link failure”:
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?
我想这是错误的URL或数据库配置,但我真的不知道要检查什么。有任何想法吗?
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 ).
编辑:我的朋友已修复我的问题,将URL中的“localhost”替换为“127.0.0.1”(这非常令人尴尬,因为你可以想象:P)。
So question is: Why is XAMPP not able to translate "localhost" into IP address and how to fix it?
所以问题是:为什么XAMPP无法将“localhost”转换为IP地址以及如何修复它?
3 个解决方案
#1
7
Why is XAMPP not able to translate "localhost" into IP address and how to fix it?
为什么XAMPP无法将“localhost”转换为IP地址以及如何修复它?
This is not a XAMPP problem nor a programming problem. This is more a DNS problem.
这不是XAMPP问题,也不是编程问题。这更像是一个DNS问题。
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)
首先,您是否有一个%SystemRoot%/ system32 / drivers / etc / hosts文件,第一行是以下行? (因此,在所有评论之后,但在任何其他主持人声明之前)
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:
更新:根据评论我已经谷歌搜索了一下,看起来MySQL JDBC驱动程序根本不吃IPv6地址。换句话说,您需要将:: 1更改为127.0.0.1。但我也发现这个主题提到您可以使用以下JVM参数来解决此问题:
java -Djava.net.preferIPv4Stack=true
#2
2
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
我试过并成功连接。首先在phpmyadmin中创建一个数据库 - 例如。 'mydb'然后在代码中将connection.url与此值放在一起
'jdbc:mysql://localhost:3306/mydb'
If you don't create a database first it wont connect
如果您不首先创建数据库,它将不会连接
#3
1
In MySql you have to allow access for your user from localhost explicitly. Here is an example (taken from here):
在MySql中,您必须允许显式地从localhost访问您的用户。这是一个例子(取自这里):
mysql> grant usage on *.* to amarokuser@localhost identified by 'amarokpasswd';
mysql> grant all privileges on amarokdb.* to amarokuser@localhost ;
#1
7
Why is XAMPP not able to translate "localhost" into IP address and how to fix it?
为什么XAMPP无法将“localhost”转换为IP地址以及如何修复它?
This is not a XAMPP problem nor a programming problem. This is more a DNS problem.
这不是XAMPP问题,也不是编程问题。这更像是一个DNS问题。
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)
首先,您是否有一个%SystemRoot%/ system32 / drivers / etc / hosts文件,第一行是以下行? (因此,在所有评论之后,但在任何其他主持人声明之前)
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:
更新:根据评论我已经谷歌搜索了一下,看起来MySQL JDBC驱动程序根本不吃IPv6地址。换句话说,您需要将:: 1更改为127.0.0.1。但我也发现这个主题提到您可以使用以下JVM参数来解决此问题:
java -Djava.net.preferIPv4Stack=true
#2
2
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
我试过并成功连接。首先在phpmyadmin中创建一个数据库 - 例如。 'mydb'然后在代码中将connection.url与此值放在一起
'jdbc:mysql://localhost:3306/mydb'
If you don't create a database first it wont connect
如果您不首先创建数据库,它将不会连接
#3
1
In MySql you have to allow access for your user from localhost explicitly. Here is an example (taken from here):
在MySql中,您必须允许显式地从localhost访问您的用户。这是一个例子(取自这里):
mysql> grant usage on *.* to amarokuser@localhost identified by 'amarokpasswd';
mysql> grant all privileges on amarokdb.* to amarokuser@localhost ;