值java.sql.SQLException:访问被拒绝的用户“根” @“本地主机”(使用密码:否)

2019-07-31 00:36发布

我有我的项目的烦恼,它涉及到JDBCMysql连接。

当我做到以下几点:

private Statement m_statement = null;               // statement 
private Connection m_connection = null;             //  connection

/**
 * Constructor #1 of the Database class
 * @throws ClassNotFoundException
 */
public Database() throws ClassNotFoundException
{
    try
    {
        Class.forName("com.mysql.jdbc.Driver");
        MysqlDataSource ds = new MysqlConnectionPoolDataSource();
        ds.setServerName("localhost");
        ds.setPort(3306);
        ds.setUser("root");
        ds.setPassword("");
        this.m_connection = ds.getConnection();
        this.m_statement = this.m_connection.createStatement();
    }

    catch (SQLException s)
    {
        System.out.println("SQL statement is not executed! 2!");
    }

}

当我尝试使用上面的代码连接,连接不能正常工作,这里是堆栈跟踪:

java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: NO)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1074)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4074)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4006)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:919)
    at com.mysql.jdbc.MysqlIO.proceedHandshakeWithPluggableAuthentication(MysqlIO.java:1694)
    at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1244)
    at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2397)
    at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2430)
    at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2215)
    at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:813)
    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:399)
    at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:334)
    at com.mysql.jdbc.jdbc2.optional.MysqlDataSource.getConnection(MysqlDataSource.java:443)
    at com.mysql.jdbc.jdbc2.optional.MysqlDataSource.getConnection(MysqlDataSource.java:141)
    at com.mysql.jdbc.jdbc2.optional.MysqlDataSource.getConnection(MysqlDataSource.java:111)
    at db.Database.<init>(Database.java:99)
    at controller.LoginPage.doGet(LoginPage.java:61)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:225)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:927)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1001)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:579)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)

我怎样才能解决这个问题?

问候

Answer 1:

我遇到了同样的错误,这是我的固定它:

我用DriverManager类是这样的:

    Class.forName("com.mysql.jdbc.Driver");
    Connection Hello = DriverManager.getConnection("jdbc:mysql://YOUR-URL-HERE", "USERNAME", "PASSWORD");

仅此而已。 另外,选择一个表时,remenber首先把数据库的名称,如:

    "DB_NAME.TABLE_NAME".


Answer 2:

要检查是否MySQL有一个密码,进入你的mysql安装目录,并在bin目录中尝试使用弹出MySQL提示

./mysql -u MySQL的root

如果登录经历没有任何问题,这意味着未设置密码。
您可以使用mysqadmin脚本设置/重置密码。

./mysqladmin -u root密码“密码字符串”



Answer 3:

您好像才刚刚mysql安装。 运行mysql_secure_installation 。 它会提示你输入根口令,等等。



Answer 4:

我有同样的问题。 您需要设置用户密码。 转到MySQL控制台。

$ mysqladmin -u root password myFirstpassword

然后在Java程序中更改您的密码



Answer 5:

无论是用户名/密码错误或用户缺少权限访问模式/表。 采用授予权限

GRANT ALL PRIVILEGES ON。 TO '根' @ 'localhost' 的WITH GRANT OPTION



文章来源: java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: NO)