在Eclipse中拒绝用户``@'localhost'(使用密码:NO)的访问,而不是'root'@'localhost',类路径已损坏 [英] Access denied for user ''@'localhost' (using password: NO) in Eclipse, instead of 'root'@'localhost', classpath corrupted

查看:76
本文介绍了在Eclipse中拒绝用户``@'localhost'(使用密码:NO)的访问,而不是'root'@'localhost',类路径已损坏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用SceneBuilder和Eclipse创建一个Java项目;我需要连接到使用MySQL创建的本地数据库,但是在Eclipse中运行Java代码后,出现以下错误:

I'm making a java project with SceneBuilder and Eclipse; i need to connect to a local database created using MySQL, but after running my Java code in eclipse i get the following error:

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:197)
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)

不是登录密码错误的'root'@'localhost'(使用密码:否),现在我有了''@'localhost'(使用密码:否).使用MySQL工作台和命令提示符,我可以使用"root"/"root"正确访问,并在数据库上启动查询.在另一台笔记本电脑或我的家用PC上运行相同的Eclipseproject,我可以毫无错误地运行它.因此问题不在于代码,而在于我当前笔记本电脑的某些设置.

Instead of 'root'@'localhost' (using password: NO) that is a login credentials error, now i have ''@'localhost' (using password: NO). Using MySQL workbench and command prompt, i can correctly access using "root"/"root", and launch queries on my db. Running the same Eclipseproject on another laptop or my home pc, i can run it without errors. So the problem is not in the code but in some setting of my current laptop.

我已经尝试过:

  • 卸载并重新安装Eclipse.
  • 卸载并重新安装mySQL产品:Workbench,Server,J Connector.
  • 切换Eclipse工作区.
  • 从Windows防火墙打开3306端口.

这是我尝试连接到数据库的代码的一部分.

That's the part of the code where i try to connect to my db.

package model;

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



public class UserDAO {

    private static String USER = "root";
    private static String PASS = "root";
    private static String DB_URL = "jdbc:mysql://127.0.0.1:3306/beecological?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC";
    private static String DRIVER_CLASS_NAME = "com.mysql.cj.jdbc.Driver";


    public static boolean checkUsername(String username) {
        Statement stmt = null;
        Connection conn = null;
        int res = 1;

        try {

            Class.forName(DRIVER_CLASS_NAME);

            conn = DriverManager.getConnection(DB_URL, USER, PASS);

            stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
                    ResultSet.CONCUR_READ_ONLY);
            res = Queries.verifyUsernameAvailable(stmt, username);

            stmt.close();
            conn.close();
        }catch (Exception e) {
            e.printStackTrace();
        }

        if(res == 1) {
            return false;
        }
        return true;
    }

    public static void saveUser(User instance) {
        Statement stmt = null;
        Connection conn = null;

        try {

            Class.forName(DRIVER_CLASS_NAME);

            conn = DriverManager.getConnection(DB_URL, USER, PASS);

            stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
                    ResultSet.CONCUR_READ_ONLY);
            Queries.insertUser(stmt, instance);

            stmt.close();
            conn.close();
        }catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static boolean verifyLogin(User instance) {
        Statement stmt = null;
        Connection conn = null;
        int res = 0;

        try {
            //caricamento driver mysql
            Class.forName(DRIVER_CLASS_NAME);

            conn = DriverManager.getConnection(DB_URL, USER, PASS);

            stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
                    ResultSet.CONCUR_READ_ONLY);
            res = Queries.verifyUserRegistered(stmt, instance);

            stmt.close();
            conn.close();
        }catch (Exception e) {
            e.printStackTrace();
        }

        if (res == 0) {
            return false;   //utente immesso non esiste
        }
        return true;
    }
}

推荐答案

解决方案

问题不是关于MySQL登录凭据,而是关于Eclipse配置.

Solution

The problem was not about MySQL login credentials, but on Eclipse configuration.

在我的文件夹项目中, .classpath .project 文件已损坏.实际上,要转到 myproject>右键单击> Run As ..> Run Configurations ,将主类设置为 com.mysql.cj.jdbc.admin.TimezoneDump.main 我的 mainApp 类.尝试在此处更改主类文件不起作用,因为对于Eclipse, mainApp 不存在.

In my folder project the .classpath and the .project files were corrupted. Infact going to myproject>right click>Run As..>Run Configurations the main class was setted as com.mysql.cj.jdbc.admin.TimezoneDump.main instead of my mainApp class. Trying to change there the main class file not worked, because for Eclipse the mainApp doesn't exist.

这就是我为解决错误所做的事情:

  1. 从Eclipse删除项目(不在磁盘上)
  2. 关闭Eclipse
  3. 转到Project文件夹并删除 .classpath .project 文件
  4. 打开eclipse,转到 File>从文件系统打开项目,然后选择项目目录
  5. 右键单击项目>运行方式>运行配置,然后设置正确的Main class
  1. Delete the project from Eclipse (not on the disk)
  2. Close Eclipse
  3. Go to Project folder and delete .classpath and .project files
  4. Open eclipse, go to File > Open projects from File System and choose the project directory
  5. Right click on the project > Run As > Run Configurations and set the right Main class

这篇关于在Eclipse中拒绝用户``@'localhost'(使用密码:NO)的访问,而不是'root'@'localhost',类路径已损坏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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