本文介绍了线程"main"中的异常java.sql.SQLException:对用户"@'localhost"的访问被拒绝(使用密码: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)

无论我做什么,都会收到此错误.我使用MySQL Workbench,更改了root用户的密码,授予了root用户的所有权限,尝试断开连接并再次连接到数据库.没关系,不管我做什么.你能帮我解决这个问题吗?我不知道该怎么办密码和用户名均正确.

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代码:

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();
    }

}

工作台

推荐答案

您的日食正在运行类"com.mysql.cj.jdbc.admin.TimezoneDump"(还包含一个主方法),而不是MainApp类.

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

尝试此操作:

Right click on MainApp.java -> Run As -> Java Application

这篇关于线程"main"中的异常java.sql.SQLException:对用户"@'localhost"的访问被拒绝(使用密码:NO)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 06:35