1

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)

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.

I already tried to:

  • Uninstall and reinstall Eclipse.
  • Uninstall and reinstall mySQL products: Workbench, Server, J Connector.
  • Switch Eclipse workspace.
  • Open the 3306 port from windows firewall.

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;
    }
}
6
  • Does this answer your question? Access denied for user 'root@localhost' (using password:NO) Commented Jan 24, 2020 at 15:32
  • Maybe, your empty user is the problem, see the error message ...: Access denied for user ''@'localhost' (using password: NO), it has an empty String as user name, doesn't it? Commented Jan 24, 2020 at 15:36
  • Yes, that's the problem, but i don't know how this is possible, because using workbench i access the database correctly using user: 'root' and psw: 'root' Commented Jan 24, 2020 at 15:48
  • If you have enabled using password: NO, you don't have to provide a password, but it is not recommended to use the root without a password. Anyway, it is weird the username is emptied somehow... You can try it without password, but I doubt it will change this behaviour. Commented Jan 24, 2020 at 15:50
  • using password: NO it's an error, i access the db correctly using cmd and workbench using root/root. I get the error also if i remove completely from my code the DriverManager.getConnection(). Commented Jan 24, 2020 at 16:12

1 Answer 1

3

Solution

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

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.

So this is what i've done to solve the error:

  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
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.