线程“main"中的异常java.sql.SQLException: 用户“@"localhost 的访问被拒绝(使用密码:否) [英] Exception in thread "main" java.sql.SQLException: Access denied for user ''@'localhost' (using password: NO)

查看:28
本文介绍了线程“main"中的异常java.sql.SQLException: 用户“@"localhost 的访问被拒绝(使用密码:否)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

}

工作台

推荐答案

您的 Eclipse 正在运行类com.mysql.cj.jdbc.admin.TimezoneDump"(其中也包含一个 main 方法),而不是您的 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 的访问被拒绝(使用密码:否)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆