0

I install mysql on Mac with account (root, mypassword). I can connect to the database by this account with "MySQL Query Browser". But, when i try to connect by Java driver, i have got an Exception : java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES)

I known that many people have the same problem as me. I read and try a lot of related topics on https://stackoverflow.com/. But it doesn't work to resolve my problem. Could you help me please ?

Thank you in advanced.

Here is my code

System.out.println("-------- MySQL JDBC Connection Testing ------------");

try {
    Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
    System.out.println("Where is your MySQL JDBC Driver?");
    e.printStackTrace();
    return;
}

System.out.println("MySQL JDBC Driver Registered!");
Connection connection = null;

try {
    connection = (Connection) DriverManager
    .getConnection("jdbc:mysql://localhost:3306/test","root", "mypassword");

} catch (SQLException e) {
    System.out.println("Connection Failed! Check output console");
    e.printStackTrace();
    return;
}

if (connection != null) {
    System.out.println("You made it, take control your database now!");
} else {
    System.out.println("Failed to make connection!");
}
Community
  • 1
  • 1
  • please list some of the approaches you have tried (http://stackoverflow.com/questions/17908273/access-denied-for-user-rootlocalhost?rq=1 for example) and your results of them. Either you have a common problem than the other answers will help. Or you have an uncommon one than the results of the other approaches will help solving. – Angelo Fuchs Oct 31 '13 at 22:51
  • Thanks. I tried it : connect to mysql from console and then run the command mentionned in this article, but it doesn't work. – Nguyen Phuc Hai Oct 31 '13 at 23:23
  • 1
    Please edit the results of your attempt into your question. List the contents of the `mysql.user` table. And please start thinking about what other information could be relevant and provide them. If you read the other questions (and answers!) carefully you will notice what gets asked often, so please, in your question, make CLEAR why thats not the case for you (Don't just say 'its not the case', prove it by adding relevant details). Every minute you spend asking good questions is ten minutes less for someone who wants to help you. – Angelo Fuchs Nov 01 '13 at 12:15

1 Answers1

0
 create user 'myUser'@'*' identified by 'mypassword';
 grant all privileges on test.* to 'myUser'@'%' identified by 'mypassword' with grant option;
 flush privileges;

On your mysql instance, try these three lines. Then in your Java code, change the login name from root to myUser.

Meesh
  • 379
  • 1
  • 13