访问被拒绝从java连接到mysql

时间:2015-04-15 13:52:18

标签: java mysql

我正在尝试从java连接数据库并收到以下错误消息:

java.sql.SQLException: Access denied for user 'gda'@'localhost' (using password: YES)

但是从命令行使用相同的凭据我是连接

server$ mysql -ugda -pgda
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2666453
Server version: 5.5.39-36.0-log Percona Server (GPL), Release 36.0, Revision 697

Copyright (c) 2009-2014 Percona LLC and/or its affiliates
Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

在另一个数据库上我的java工作正常且能够连接。似乎是数据库级别的问题?!我需要在数据库级别修复什么?

添加连接代码:

public Connection getConnection() {     
    try {
        Class.forName(jdbcDriver);

        connection = DriverManager.getConnection(connectionString());
    }
    catch(Exception ex) {
        ex.printStackTrace();
        logger.error("Exception:" + ex.getMessage());
        errorMessage = ex.getMessage();
    }

    return connection;
}

其中:

jdbc:mysql://127.0.0.1:3306/db?user=gda&password=gda

1 个答案:

答案 0 :(得分:0)

尝试以这种方式传递用户名和密码:

String DB_URL = "jdbc:mysql://localhost/xyz";
String USER = "username";
String PASS = "password";

conn = DriverManager.getConnection(DB_URL,USER,PASS);