1

So I'm getting the error java.sql.SQLException: Access denied for user ''@'localhost' (using password: NO) when I try to connect to MySQL Database from Java code. Everything I was able to find didn't solve the Problem at all and the code is very basic. I used it in projects before and found it like that in many tutorials. So first the relevant code:

//Inside class that manages SQLConnection
public SQLTransceiver(String url){
        this.connectionURL = url;
        this.reconnect();
}

public void reconnect(){
        try {
            Connection connection = DriverManager.getConnection(this.connectionURL); //Where the error occurs
            this.currentConnection = connection;
        } catch (SQLException e) {
            e.printStackTrace();
        }
}

//Inside main class I call this for testing purpose
String s = "jdbc:mysql://localhost:3306/tableName" +
                "?user=user" +
                "&password=thePassword" +
                "&serverTimezone=MET";

DataTransreciever transreciever = new SQLTransceiver(s);

NOTE: I can perfectly log into the DB in MySQL workbench and do everything there from creating/altering tables to inserting and all that stuff.

The whole Stacktrace looks like this:

java.sql.SQLException: Access denied for user ''@'localhost' (using password: NO)
    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.DriverManager.getConnection(DriverManager.java:664)
    at java.sql.DriverManager.getConnection(DriverManager.java:208)
    at main.data.SQLTransceiver.reconnect(SQLTransceiver.java:31)
    at main.data.SQLTransceiver.<init>(SQLTransceiver.java:20)
    at main.ui.toDoPane.ToDoPane.<init>(ToDoPane.java:208)
    at main.ui.Window.start(Window.java:40)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$161(LauncherImpl.java:863)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$174(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$null$172(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$173(PlatformImpl.java:294)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$147(WinApplication.java:177)
    at java.lang.Thread.run(Thread.java:748)

So what did I do to fix this: It made me wonder why it doesn't state which user was denied note it statets "denied for user ''@'localhost'. So I did put in wrong password and this error came:

java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES)
    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.DriverManager.getConnection(DriverManager.java:664)
    at java.sql.DriverManager.getConnection(DriverManager.java:208)
    at main.data.SQLTransceiver.reconnect(SQLTransceiver.java:31)
    at main.data.SQLTransceiver.<init>(SQLTransceiver.java:26)
    at main.Main.main(Main.java:29)

So it looks like it at some point does check the user and password. It just gives the first error if user and password are correct.

So I did some lookup and got the solution that is stated for this question a few times (Although often it does state the "Using password: Yes" insted of "No"):

java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES)

It states I should run following statement:

GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' IDENTIFIED BY '%password%' WITH GRANT OPTION;

The Problem is I can't run that. When I try to run it in MySQL Workbench I get the following error:

0   5   01:12:32    GRANT ALL PRIVILEGES ON *.* TO 'user'@'localhost' IDENTIFIED BY 'password' WITH GRANT OPTION    Error Code: 1064. You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'IDENTIFIED BY 'password' WITH GRANT OPTION' at line 1  0.000 sec

Additionally I tried to remove MySQL completely and reinstall it. This didn't help.

Disclaimer: Password and user was changed for privacy reasons. In terms of errors it doesn't make a difference calling the DriverManager with connection String and Properties class I did try that to. Sorry for more basic and maybe bad englisch. I am using MySQL Server 8.0.20 Community with InnoDB Engine. The J/Connector and MySQL Notifier are for the same Version. I installed all (including the workbench) with the Windows Installer downloaded from: https://dev.mysql.com/downloads/mysql/ I am using Windows 10.

When I remove the "serverTimezone" parameter I get the following error:

java.sql.SQLException: The server time zone value 'Mitteleurop�ische Sommerzeit' is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the 'serverTimezone' configuration property) to use a more specifc time zone value if you want to utilize time zone support.
    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.SQLError.createSQLException(SQLError.java:89)
    at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:63)
    at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:73)
    at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:76)
    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.DriverManager.getConnection(DriverManager.java:664)
    at java.sql.DriverManager.getConnection(DriverManager.java:208)
    at main.data.SQLTransceiver.reconnect(SQLTransceiver.java:31)
    at main.data.SQLTransceiver.<init>(SQLTransceiver.java:26)
    at main.Main.main(Main.java:28)

I don't know if that helps, but it's possible.

Thanks very much in advance :)

Shadow
  • 33,525
  • 10
  • 51
  • 64
TyjameTheNeko
  • 116
  • 1
  • 9
  • Does your database have a password? From the looks of the stack trace the credentials you're using in your code to connect to it do not provide a password. – notacorn May 29 '20 at 23:41
  • @notacorn it should. At least I use one to log in with the Workbench. – TyjameTheNeko May 30 '20 at 00:26

2 Answers2

1

So what at least fixed the problem was to reinstall MySQL Server again.

The difference was this time I made sure to choose the 'new' password encryption ("caching_sha2_password") for every component (Server, Connector/J, Notifier and Workbench).

Altough I don't understand how this would cause an error like this or if that even was the error it fixed it.

Thanks for everyone who did there best to help :D

TyjameTheNeko
  • 116
  • 1
  • 9
0

Please use the following connection url:

 String s = "jdbc:mysql://localhost:3306/databasename?user=user&password=password&useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC";

I hope it's help you, thanks.