java jdbc 链接本地mysql数据库 报错 Access denied for user 'root'@'localhost' (using password: YES)
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.</span><span style="color: rgba(0, 0, 255, 1)">out</span>.println(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">Success connect Mysql server!</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">); Statement stmt </span>=<span style="color: rgba(0, 0, 0, 1)"> connect.createStatement(); ResultSet rs </span>= stmt.executeQuery(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">select * from user</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">user 为你表的名称</span> <span style="color: rgba(0, 0, 255, 1)">while</span><span style="color: rgba(0, 0, 0, 1)"> (rs.next()) { System.</span><span style="color: rgba(0, 0, 255, 1)">out</span>.println(rs.getString(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">name</span><span style="color: rgba(128, 0, 0, 1)">"</span>)+rs.getString(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">password</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">)); } } </span><span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)"> (Exception e) { System.</span><span style="color: rgba(0, 0, 255, 1)">out</span>.print(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">get data error!</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">); e.printStackTrace(); } }
}
----------------- 那么原因究竟在哪里? ---------------------
4. 我想了一下,用户名,密码肯定没错,jar 包 也 buildPath 进来了,会不会是我的 mysql 服务没有开起来???
------------------- 问题就这样解决了 -----------------
5. ----------------- 如果文章对您有所帮助,欢迎打赏 --------------