Exception in thread “main” java.sql.SQLException: Access denied for user ''@'localhost' (using password: NO)

问题


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


回答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


来源:https://stackoverflow.com/questions/59225429/exception-in-thread-main-java-sql-sqlexception-access-denied-for-user-loc

标签

更多相关内容:请点击查看