How to connect local XAMPP MySQL database using JDBC?

I have this Tetris game written in Java that uses a database to record high results. It worked fine while I used the remote MySQL DB, but now I'm trying to configure localhost DB using XAMPP MySQL, and it continues to work as "SQLException: Network Communication Error" with the command:

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

I assume this is the wrong URL or DB configuration, but I really don't know what to check. Any ideas?

EDIT: my friend fixed my problem by replacing "localhost" in the URL with "127.0.0.1" (which was pretty awkward, as you might imagine: P).

So the question is: Why cannot XAMPP translate "localhost" into an IP address and how to fix it?

+7
java mysql jdbc xampp
source share
3 answers

Why can't XAMPP translate "localhost" into an IP address and how to fix it?

This is not a XAMPP problem and a programming problem. This is more of a DNS problem.

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

 127.0.0.1 localhost 

Update: according to Googled's comments, and it looks like the MySQL JDBC driver is not eating IPv6 . In other words, you need to change ::1 to 127.0.0.1 . But I also found this section that mentions that you can use the following JVM argument to fix this problem:

 java -Djava.net.preferIPv4Stack=true 
+7
source share

I tried and got a successful connection. First create a database in phpmyadmin - for example. 'MYDB' and then in the code put connection.url with this value

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

If you do not create the database, it will not connect at first

+2
source share

In MySql, you must explicitly allow access for your user from localhost. 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
source share

All Articles