Java 如何使用 JDBC 连接 XAMPP MySQL 本地数据库?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2168969/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 04:22:27  来源:igfitidea点击:

How to connect XAMPP MySQL local DB using JDBC?

javamysqljdbcxampp

提问by Jakub Stejskal

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:通信链接失败”一样:

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 地址以及如何修复它?

采纳答案by BalusC

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/hostsfile with the following line as firstline? (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 Googleda bit and it look like that the MySQL JDBC driver doesn't eat IPv6addresses at all. In other words, you'll need to change ::1to 127.0.0.1. But I also found this topicwhich mentions that you can use the following JVM argument to fix this problem:

更新:根据我在谷歌上搜索过的评论,看起来 MySQL JDBC 驱动程序根本不占用IPv6地址。换句话说,您需要更改::1127.0.0.1. 但我也发现这个话题提到你可以使用以下 JVM 参数来解决这个问题:

java -Djava.net.preferIPv4Stack=true 

回答by tangens

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 ;

回答by bhv

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

如果您不先创建数据库,它将无法连接