Access denied for user 'root'@'localhost'

Access denied for user 'root'@'localhost'

直接当的demo1也会报这个错





代码如下


package com.imooc.jdbc.demo1;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import org.junit.Test;

import com.mysql.jdbc.Driver;

public class JDBCDemo1 {
    
    @Test
    /**
     * JDBC资源的释放
     */
    public void demo2(){
        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;
        try {
            // 1.加载驱动
//            DriverManager.registerDriver(new Driver());// 会导致驱动注册两次。
            Class.forName("com.mysql.jdbc.Driver");
            // 2.获得连接    该处参数没有错误
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/imooc", "root", "##@#¥@");
            // 3.创建执行SQL语句的对象,并且执行SQL
            // 3.1创建执行sql的对象
            String sql = "select * from user";
            stmt = conn.createStatement();
            // 3.2执行sql
            rs = stmt.executeQuery(sql);
            while(rs.next()){
                int uid = rs.getInt("uid");
                String username = rs.getString("username");
                String password = rs.getString("password");
                String name = rs.getString("name");
                
                System.out.println(uid+"   "+username+"   "+password+"   "+name);
            }
            
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            // 4.释放资源
            if (rs != null) {
                try {
                    rs.close();
                } catch (SQLException sqlEx) { // ignore
                    
                }

                rs = null;
            }
            
            if(stmt != null){
                try {
                    stmt.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
                stmt = null;
            }
            
            if(conn != null){
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
                conn = null;// 垃圾回收机制更早回收对象。
            }
        }
    }

    @Test
    /**
     * JDBC的入门程序
     */
    public void demo1(){
        
        try {
            // 1.加载驱动
//            DriverManager.registerDriver(new Driver());// 会导致驱动注册两次。
            Class.forName("com.mysql.jdbc.Driver");
            // 2.获得连接
            Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/imooc", "root", "1542986730");
            // 3.创建执行SQL语句的对象,并且执行SQL
            // 3.1创建执行sql的对象
            String sql = "select * from user";
            Statement stmt = conn.createStatement();
            // 3.2执行sql
            ResultSet resultSet = stmt.executeQuery(sql);
            while(resultSet.next()){
                int uid = resultSet.getInt("uid");
                String username = resultSet.getString("username");
                String password = resultSet.getString("password");
                String name = resultSet.getString("name");
                
                System.out.println(uid+"   "+username+"   "+password+"   "+name);
            }
            // 4.释放资源
            resultSet.close();
            stmt.close();
            conn.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}



控制台输出及报错信息如下 

1   aaa   111   张三
2   bbb   222   李四
3   ccc   333   王五
java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1055)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3515)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3447)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:911)
    at com.mysql.jdbc.MysqlIO.secureAuth411(MysqlIO.java:3953)
    at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1276)
    at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2048)
    at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:723)
    at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:46)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:406)
    at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:302)
    at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:282)
    at java.sql.DriverManager.getConnection(DriverManager.java:664)
    at java.sql.DriverManager.getConnection(DriverManager.java:247)
    at com.imooc.jdbc.demo1.JDBCDemo1.demo2(JDBCDemo1.java:28)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:538)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:760)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:460)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:206)






正在回答 回答被采纳积分+1

登陆购买课程后可参与讨论,去登陆

3回答
JeasonLeeHao 2018-07-20 13:09:25

昨天弄得时候跟你一样的报错,而且控制台还不输出信息,然后从网上找了下。直接从装MySQL就行了, 先把MySQL卸载干净,然后安装好之后再设置初始密码。再来运行就没问题了!希望对你帮助!

Lawrence 2018-07-19 10:26:12

提供这个帖子给同学参考一下。

https://www.cnblogs.com/wozixiaoyao/p/5751108.html

一叶知秋519 2018-07-17 13:38:29

建议同学检查一下JDBC的数据库链接密码是否正确~

祝学习愉快!

问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
Java数据库开发与实战应用2018版
  • 参与学习           人
  • 提交作业       277    份
  • 解答问题       4297    个

Java数据库开发的必备技能,从流行的MySQL数据库开始,到Java原生的数据库管理接口JDBC的使用,再到常用的数据持久化框架MyBatis,让你向Java工程师的目标又迈进了一步!

了解课程
请稍等 ...
意见反馈 帮助中心 APP下载
官方微信

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师