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

原文链接: http://www.cnblogs.com/GG-Bond/p/9217193.html

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

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

环境:win10  +  mysql8.0.11 +  eclipse

1.代码

 1 import java.sql.*;
 2 
 3 public class SqlConnection {
 4     private static final String URL = "jdbc:mysql://127.0.0.1:3306/test?useSSL=false&serverTimezone=GMT";
 5     private static final String NAME = "root";
 6     private static final String PASSWORD = "root";
 7     public void TheSqlConnection() {
 8         try {
 9             Class.forName("com.mysql.cj.jdbc.Driver");
10             System.out.println("连接成功");
11         } catch (ClassNotFoundException e) {
12             System.out.println("连接失败");
13             e.printStackTrace();
14         }
15         Connection conn = null;
16         try {
17             System.out.println("开始获取");
18             conn = DriverManager.getConnection(URL, NAME, PASSWORD);
19             System.out.println("获取成功");
20         } catch (SQLException e) {
21             System.out.println("获取失败");
22             e.printStackTrace();
23         }
24         if (conn != null) {
25             try {
26                 conn.close();
27             } catch (SQLException e) {
28                 e.printStackTrace();
29                 conn = null;
30             }
31         }
32     }
33 }
1 public class MainClass {
2 
3     public static void main(String[] args) {
4         // TODO Auto-generated method stub
5         new SqlConnection().TheSqlConnection();
6     }
7 
8 }

2.项目结构

java.sql.SQLException: Access denied for user 'roo'@'localhost' (using password: YES)_第1张图片

已经导入驱动包

3.错误信息

java.sql.SQLException: Access denied for user 'roo'@'localhost' (using password: YES)_第2张图片

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)_第3张图片

5.navicat也可以连接上

java.sql.SQLException: Access denied for user 'roo'@'localhost' (using password: YES)_第4张图片

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

 

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

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

java.sql.SQLException: Access denied for user 'roo'@'localhost' (using password: YES)_第5张图片

转载于:https://www.cnblogs.com/GG-Bond/p/9217193.html

你可能感兴趣的:(java.sql.SQLException: Access denied for user 'roo'@'localhost' (using password: YES))