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

时间:2022-03-31 16:01:15

初学mysql,安装了mysql8.0.11,激动的用jdbc连接数据库,出现error,折腾了三天依旧无解,最后无奈装了比较稳定的mysql5.5,问题得以解决,很迷,但只要error没了就开心。

在此记录我傻傻坚持的3天。

环境:win10  +  mysql8.0.11 +  eclipse

1.代码

 import java.sql.*;

 public class SqlConnection {
private static final String URL = "jdbc:mysql://127.0.0.1:3306/test?useSSL=false&serverTimezone=GMT";
private static final String NAME = "root";
private static final String PASSWORD = "root";
public void TheSqlConnection() {
try {
Class.forName("com.mysql.cj.jdbc.Driver");
System.out.println("连接成功");
} catch (ClassNotFoundException e) {
System.out.println("连接失败");
e.printStackTrace();
}
Connection conn = null;
try {
System.out.println("开始获取");
conn = DriverManager.getConnection(URL, NAME, PASSWORD);
System.out.println("获取成功");
} catch (SQLException e) {
System.out.println("获取失败");
e.printStackTrace();
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
conn = null;
}
}
}
}
 public class MainClass {

     public static void main(String[] args) {
// TODO Auto-generated method stub
new SqlConnection().TheSqlConnection();
} }

2.项目结构

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

已经导入驱动包

3.错误信息

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

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

出现这个错误一般就是用户名或者密码错误了,经多次检查用户名和密码正确。

4.命令行可以登陆

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

5.navicat也可以连接上

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

错误发生处:DriverManager.getConnection(URL, NAME, PASSWORD);

这个问题是真的皮,问朋友问学长问老师,能问的都请教了遍,都没解决,心累啊。

最后的最后,卸载干净mysql8.0.11,重装上mysql5.5.重新运行代码,问题没了o(* ̄▽ ̄*)o

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