java jdbc 链接本地mysql数据库 报错 Access denied for user 'root'@'localhost' (using password: YES)

时间:2021-06-30 19:14:32

1.--------------问题描述----------------------------

java jdbc 链接本地mysql数据库 报错  Access denied for user 'root'@'localhost' (using password: YES)

 

2. ------------解决过程---------------------------------------

01.最开始是因为jdbc驱动包导进来,倒入驱动包

02.导入之后,报错

Access denied for user 'root'@'localhost' (using password: YES)

 就感觉很奇怪了,因为命令窗口里用户名和密码是可以等进去的,然后网上搜了一下

说:用户权限不够,环境变量配置,jar包都可能导致这个错误。。。

3. 代码肯定是没有错的

package my01;
import java.sql.
*;
public class jdbctest {
public static void main(String args[]) {
String pwd
= "123456";
try {
Class.forName(
"com.mysql.jdbc.Driver"); //加载MYSQL JDBC驱动程序
//Class.forName("org.gjt.mm.mysql.Driver");
System.out.println("Success loading Mysql Driver!");
}
catch (Exception e) {
System.
out.print("Error loading Mysql Driver!");
e.printStackTrace();
}
try {
Connection connect
= DriverManager.getConnection(
"jdbc:mysql://localhost:3306/test","root",pwd);
//连接URL为 jdbc:mysql//服务器地址/数据库名 ,后面的2个参数分别是登陆用户名和密码


System.
out.println("Success connect Mysql server!");
Statement stmt
= connect.createStatement();
ResultSet rs
= stmt.executeQuery("select * from user");
//user 为你表的名称
while (rs.next()) {
System.
out.println(rs.getString("name")+rs.getString("password"));
}
}
catch (Exception e) {
System.
out.print("get data error!");
e.printStackTrace();
}
}
}

-----------------那么原因究竟在哪里? ---------------------

4. 我想了一下,用户名,密码肯定没错,jar包 也buildPath 进来了,会不会是我的mysql服务没有开起来???

java jdbc 链接本地mysql数据库 报错  Access denied for user 'root'@'localhost' (using password: YES)

-------------------问题就这样解决了-----------------

5. -----------------如果文章对您有所帮助,欢迎打赏--------------