java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES)

31,231

java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES)

As you have mentioned problem is with creating connection with the database itself. Statement

con = DriverManager.getConnection(DatabaseURL, userName, password);

is throwing exception which is getting caught at

catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }   

after which you are returning dbresult which you have initialized to null(which you have to as it is a local variable). So the problem is that you are unable to connect to your db due to authentication.

On your machine where (localhost in ur case ) run the following command from console

GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' IDENTIFIED BY 'root' WITH GRANT OPTION;
Share:
31,231
QGA
Author by

QGA

Updated on March 26, 2020

Comments

  • QGA
    QGA about 4 years

    Why do you think this function gives me always null?? Using another login program, which has the same code structure, it works greatly. My DBURL is jdbc:mysql://localhost:8889/database

        public String retrieveUserPassword(String userName, String password) {
    
        String query = "SELECT UserPassword FROM Access where UserName='"+userName+"'";
        String dbresult=null; //this might be the problem, but I must define it
    
        try {
          Class.forName("com.mysql.jdbc.Driver");
    
        }catch (ClassNotFoundException ex1) {
    
          System.out.println("Driver could not be loaded: " + ex1);
          Logger.getLogger(DatabaseModel.class.getName()).log(Level.SEVERE, null, ex1);
        }
        try {
    
              //These are private variables declared at the top of the class 
              //and used by various functions   
              con = DriverManager.getConnection(DatabaseURL, userName, password);   
              st = con.createStatement(); 
              rs = st.executeQuery(query);
    
                if(rs.next()){
    
                    dbresult= rs.getString(3);
                }
    
         }catch (SQLException e) {
    
          e.printStackTrace();
         }    
    
      return dbresult;
    
    }
    

    The Access table is composed of three columns: UserID, UserName, UserPassword

  • QGA
    QGA about 10 years
    I love you, however, I have not got why it worked with my previous code without granting access