I am trying to write a Java application that connects to a MySQL database. The OS I am using is Linux Mint (in case it might be relevant for my problem) and I downloaded the JDBC driver for indipendent platform from the official site (I am using mysql-connector-java-8.0.20.jar
).
My java code is this:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.PreparedStatement;
import java.util.Properties;
import java.sql.SQLException;
public class Driver{
public static void main(String[] args){
try{
// 1) GET CONNECTION
//Class.forName("com.mysql.cj.jdbc.Driver");
Connection myConn = DriverManager.getConnection("jdbc:mysql://localhost/db_name", "user", "pass");
// 2) CREATE STATEMENT
Statement myStmt = myConn.createStatement();
// 3) EXECUTE SQL QUERY
ResultSet myRs = myStmt.executeQuery("SELECT * FROM ARENA");
// 4) PROCESS THE RESULT SET
while (myRs.next()){
System.out.println(myRs.getString("Nome_Arena") + myRs.getString("Stato")
+ myRs.getString("Citta") + myRs.getInt("N_posti") );
}
}
catch(Exception exc){
exc.printStackTrace();
}
}
}
where, precisely, I substituted in line
Connection myConn = DriverManager.getConnection("jdbc:mysql://localhost/Wrestling", "user", "pass");
the following things:
db_name
with the real name of my database;
user
with a real username (I tried EVERY username, included root. Same problem).
pass
with the corresponding user password (precisely, I typed the passwords I set in MySQL at the first launch of the software when I installed it, but since they weren't working I even tried to use the passwords of Linux accounts, but it didn't work either)
From terminal I type this:
javac -classpath .:mysql-connector-java-8.0.20.jar Driver.java
java -classpath .:mysql-connector-java-8.0.20.jar Driver
and I get the following ERROR MESSAGE:
java.sql.SQLException: Access denied for user 'root'@'localhost'
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:129)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97)
at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122)
at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:836)
at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:456)
at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:246)
at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:197)
at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:677)
at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:228)
at Driver.main(Driver.java:22)
I already looked for possible solutions online, and I tried the following things:
1) I tried to set permissions (in case something was wrong) for both root and the other user I have on the system:
mysql> GRANT ALL PRIVILEGES ON db_name.* TO 'user'@'localhost';
mysql> GRANT ALL PRIVILEGES ON db_name.* TO 'root'@'localhost';
2) I tried to view all grants on the db:
SHOW GRANTS; (I was logged as root)
+---------------------------------------------------------------------+
| Grants for root@localhost |
+---------------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' WITH GRANT OPTION |
| GRANT ALL PRIVILEGES ON `db_name`.* TO 'root'@'localhost' |
| GRANT PROXY ON ''@'' TO 'root'@'localhost' WITH GRANT OPTION |
+---------------------------------------------------------------------+
+------------------------------------------------------+
| Grants for user@% | (is it normal this @% I got?)
+------------------------------------------------------+
| GRANT USAGE ON *.* TO 'user'@'%' |
| GRANT ALL PRIVILEGES ON `db_name`.* TO 'user'@'%' |
+------------------------------------------------------+
2 rows in set (0.00 sec)
3) someone in another post suggested to put the 3 strings (url, username and password) that are arguments of the connection in separated variables and calling the method passing thos variables as arguments, but didn't work.
I tried to be as much detailed as I could, exposing my problem. What can I do guys? I really spent hours trying to fix this, I have no idea what I could try next.