java.sql.SQLException:用户’root @ localhost’@’localhost’拒绝访问(使用密码:YES)

前端之家收集整理的这篇文章主要介绍了java.sql.SQLException:用户’root @ localhost’@’localhost’拒绝访问(使用密码:YES)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我使用EclipseMySQL db连接Java

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();
        }
    }
}

错误

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.MysqL.jdbc.JDBC4Connection.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)

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

最佳答案
而不是使用:

 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);
...

这应该工作(假设你设置了有效的密码)

原文链接:https://www.f2er.com/mysql/433208.html

猜你在找的MySQL相关文章