Java中的Mysql连接:用户@ localhost拒绝访问

时间:2014-03-15 00:26:07

标签: java mysql

我无法连接到数据库,但我不知道为什么,因为用户名和密码是正确的,我可以在本地通过phpmyadmin连接。

  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
  }

每当我呼叫连接时,我都会得到这个堆栈跟踪:

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)

2 个答案:

答案 0 :(得分:1)

你需要设置yerobot @ localhost和yerobot @'%'的密码

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

答案 1 :(得分:0)

您输入的用户名或密码错误,或者未授权从localhost连接。就那么简单。从异常中可以清楚地看出来。