Java中的MySQL连接:用户@ localhost的访问被拒绝 [英] Mysql Connection in Java: Access denied for user @ localhost

查看:637
本文介绍了Java中的MySQL连接:用户@ localhost的访问被拒绝的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法连接到数据库,但我不知道为什么,因为用户名和密码正确,并且能够通过本地phpmyadmin进行连接.

I'm not able to connect to the Database but I don't know why, since the username and password are correct and I'm able to connect through phpmyadmin locally.

  Class.forName("com.mysql.jdbc.Driver");
  // Setup the connection with the DB
  connect = DriverManager.getConnection(
            "jdbc:mysql://localhost/yerobot", "yerobot", "yerobot123");

  preparedStatement = connect.prepareStatement("select * from userdata");

  resultSet = preparedStatement.executeQuery();
  while(resultSet.next()){
    // do read rs
  }

每当我要求建立连接时,都会得到以下堆栈跟踪:

Whenever I call for a connection, I get this stacktrace:

java.sql.SQLException: Access denied for user 'yerobot'@'localhost' (using password: YES)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1086)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4237)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4169)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:928)
    at com.mysql.jdbc.MysqlIO.proceedHandshakeWithPluggableAuthentication(MysqlIO.java:1750)
    at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1290)
    at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2502)
    at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2535)
    at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2320)
    at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:834)
    at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:46)
    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:416)
    at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:347)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at yerobot.MySQLAccess.setZockerzaster(MySQLAccess.java:92)
    at yerobot.MyBot.zasterAction(MyBot.java:102)
    at yerobot.MyBot$1.run(MyBot.java:94)
    at java.util.TimerThread.mainLoop(Unknown Source)
    at java.util.TimerThread.run(Unknown Source)

推荐答案

您需要同时设置yerobot @ localhost和yerobot @'%'的密码

You need to set the password for both yerobot@localhost and yerobot@'%'

MySQL区分本地访问密码和远程访问密码.

MySQL differentiates local access and remote access passwords.

这篇关于Java中的MySQL连接:用户@ localhost的访问被拒绝的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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