0

我使用帐户(root,mypassword)在 Mac 上安装 mysql。我可以通过此帐户使用“MySQL 查询浏览器”连接到数据库。但是,当我尝试通过 Java 驱动程序连接时,出现异常:java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES)

我知道很多人和我有同样的问题。我在https://stackoverflow.com/上阅读并尝试了很多相关主题。但它不能解决我的问题。请问你能帮帮我吗 ?

提前谢谢你。

这是我的代码

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!");
}
4

1 回答 1

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

在您的 mysql 实例上,尝试这三行。然后在您的 Java 代码中,将登录名从 root 更改为 myUser。

于 2013-10-31T22:54:37.573 回答