2

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

6
  • doesn't the title answer the question? Commented Mar 19, 2014 at 14:27
  • Debug and check if query is actually returning any value. i.e if(rs.next()) is true. Commented Mar 19, 2014 at 14:27
  • @davida. a. As I mentioned, the code is right because I tried it in another program just copying and pasting, and I did not have this problem. The database always grants the access to the root user. Commented Mar 19, 2014 at 14:29
  • @Aniket Thakur yes it does Commented Mar 19, 2014 at 14:31
  • Try to print userName and password right before the getConnection() call and check if they're really correct. Commented Mar 19, 2014 at 14:32

1 Answer 1

7

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

1 Comment

I love you, however, I have not got why it worked with my previous code without granting access

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.