如何使用 JDBC 连接 XAMPP MySQL 本地数据库? [英] How to connect XAMPP MySQL local DB using JDBC?

查看:35
本文介绍了如何使用 JDBC 连接 XAMPP MySQL 本地数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个用 Java 编写的俄罗斯方块游戏,它使用 DB 来记录高分.只要我使用远程 MySQL 数据库,它就可以正常工作,但现在我正在尝试使用 XAMPP MySQL 设置 localhost 数据库,并且它在命令中一直像SQLException:通信链接失败"一样:

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);

我猜是错误的 URL 或数据库配置,但我真的不知道要检查什么.有什么想法吗?

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

我的朋友解决了我的问题,将 URL 中的localhost"替换为127.0.0.1"(这很尴尬,你可以想象 :P).

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 ).

所以问题是:为什么 XAMPP 无法将localhost"转换为 IP 地址以及如何解决?

推荐答案

为什么 XAMPP 无法将localhost"翻译成 IP 地址以及如何解决?

这不是 XAMPP 问题,也不是编程问题.这更像是一个 DNS 问题.

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

首先,您是否有一个 %SystemRoot%/system32/drivers/etc/hosts 文件,其中以下行作为 first 行?(因此,在所有评论之后,但在任何其他主机声明之前)

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

更新: 根据我的评论 谷歌了一下,看起来 MySQL JDBC 驱动程序不吃 IPv6地址.换句话说,您需要将 ::1 更改为 127.0.0.1.但是我也发现 this topic 里面提到可以使用下面的JVM解决这个问题的论据:

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 

这篇关于如何使用 JDBC 连接 XAMPP MySQL 本地数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆