java.sql.SQLException:用户'root @ localhost'@'localhost'拒绝访问(使用密码:YES) [英] java.sql.SQLException: Access denied for user 'root@localhost'@'localhost' (using password: YES)

查看:1046
本文介绍了java.sql.SQLException:用户'root @ localhost'@'localhost'拒绝访问(使用密码:YES)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Eclipse与MySQL db连接Java

I am connecting Java using Eclipse with MySQL db

代码

import java.sql.*;
import java.io.*;

public class DbDemo {

    public static void main(String args[]) throws ClassNotFoundException, SQLException {

        String s;       
        String uname="root@localhost";
        String url="jdbc:mysql://localhost:3306/student";

        String password="Hsun123";

        int i;

        try {

            Class.forName("com.mysql.jdbc.Driver").newInstance();

            Connection con=DriverManager.getConnection(url,uname,password);

            Statement st=con.createStatement();

            ResultSet rs=st.executeQuery("select * from student_detail");

            if(rs.next()) {

                i=rs.getInt(1);

                s=rs.getString(2);

                System.out.println(i+"/t"+s);
            }           

            rs.close();

            st.close();

            con.close();

        } catch(Exception e) {
            e.printStackTrace();
        }
    }
}

ERROR

java.sql.SQLException: Access denied for user 'root@localhost'@'localhost' (using password: YES)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1073)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3597)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3529)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:935)
at com.mysql.jdbc.MysqlIO.secureAuth411(MysqlIO.java:4101)
at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1300)
at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2337)
at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2370)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2154)
at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:792)
at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:47)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:381)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:305)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at database.DbDemo.main(DbDemo.java:13)

我该怎么做才能解决我的问题?

What should I do to resolve my problem?

推荐答案

而不是使用:

 String uname="root@localhost";

使用:

String url="jdbc:mysql://localhost:3306/student";
String userName="root"
String password="Hsun123"
...
try{

        Class.forName("com.mysql.jdbc.Driver").newInstance();

        Connection con=DriverManager.getConnection(url,username,password);
...

这应该有效(假设您设置的是有效密码)

This should work (provided you are setting the valid password)

这篇关于java.sql.SQLException:用户'root @ localhost'@'localhost'拒绝访问(使用密码:YES)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆