0
Exception in thread "main" 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:199)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at com.mysql.cj.jdbc.admin.TimezoneDump.main(TimezoneDump.java:70)

I get this error no matter what I do. I use MySQL Workbench, I've changed the password for the root user, I've granted all rights to the user root, I've tried to disconnect and connect again to the database. Nothing, no matter what I do. Could you please help me solve this? I don't know what else to do. The password an username are both correct.

Java code:

package Restaurant;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;


public class MainApp {
    public void main(String[] args) throws SQLException
    {

        String url="jdbc:mysql://localhost:3306/new_schema";
        Statement sql;

        ResultSet rs;
        Connection con=DriverManager.getConnection(url, "root", "root");

        sql=(Statement)con.createStatement();
        rs=sql.executeQuery("select * from restaurant");
        while(rs.next())
        {
            System.out.println("Nume: "+rs.getString("nume")+", Specific: "+rs.getString("specificul")+", Zona: "+rs.getString("zona"));
        }


        rs.close();
        sql.close();
        con.close();
    }

}

Workbench

6
  • Did you set the root account / password for "localhost" (127.0.0.1), or for some other hostname? Commented Dec 7, 2019 at 11:35
  • For what it is worth, the exception message is saying that no password was supplied, and that the user name was the empty string. Commented Dec 7, 2019 at 11:36
  • @StephenC Yes, I set it for local host, I've put a SS in the post with Administration-Users and Privileges Commented Dec 7, 2019 at 11:47
  • Please do not post code and error messages as screenshots. Commented Dec 7, 2019 at 11:59
  • 1
    DriverManager.getConnection(url, "root", "root"); doesn't seem to match Access denied for user ''@'localhost' (using password: NO). Are you sure that this code is what generates this exception? Commented Dec 7, 2019 at 12:15

1 Answer 1

1

Your eclipse is running the class "com.mysql.cj.jdbc.admin.TimezoneDump" (which also contain a main method) instead of your MainApp class.

Try this manipulation:

Right click on MainApp.java -> Run As -> Java Application
Sign up to request clarification or add additional context in comments.

3 Comments

I tried to do this, but I don't have the option Run as Java Application, I only have Run in Server.
Ok, I found the problem. Your main method is not correct. -> public static void main(String[] args)
Wow, thanks a lot! I didn't even notice! It works now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.