“ java.sql.SQLException:用户'root'@'localhost'的访问被拒绝(使用密码:是)”

时间:2019-11-22 13:20:08

标签: java database connectivity

无法连接到我的数据库。这给我一个错误消息“ java.sql.SQLException:用户'root'@'localhost'的访问被拒绝(使用密码:是)”

这是我的代码:

package youtubetest;
import java.sql.*;

public class YoutubeTest {

        private static final String USERNAME = "root";
        private static final String PASSWORD = "root";
        private static final String CONN_STRING = "jdbc:mysql://localhost:3306/youtube";

        public static void main(String args []) {
            Connection conn = null;

            try {
                conn = DriverManager.getConnection(CONN_STRING, USERNAME, PASSWORD);
                System.out.println("Connected!");
            }catch (SQLException e) {
                System.err.println(e);
            }
        }
}

1 个答案:

答案 0 :(得分:0)

在您的代码中似乎没有问题。 您是否尝试过手动连接到数据库以检查此用户名和密码是否正确?

如果使用Windows,请参见以下内容: https://phoenixnap.com/kb/connect-to-mysql-windows-command-line

或者Debian / Ubuntu: https://www.cyberciti.biz/faq/how-to-connect-to-my-mysql-database-server-using-command-line-and-php/