gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 01:16:41 30 4
gpt4 key购买 nike

为什么你认为这个函数给我的总是空值??使用另一个具有相同代码结构的登录程序,效果非常好。我的 DBURL 是 jdbc:mysql://localhost:8889/database

    public String retrieveUserPassword(String userName, String password) {

String query = "SELECT UserPassword FROM Access where UserName='"+userName+"'";
String dbresult=null; //this might be the problem, but I must define it

try {
Class.forName("com.mysql.jdbc.Driver");

}catch (ClassNotFoundException ex1) {

System.out.println("Driver could not be loaded: " + ex1);
Logger.getLogger(DatabaseModel.class.getName()).log(Level.SEVERE, null, ex1);
}
try {

//These are private variables declared at the top of the class
//and used by various functions
con = DriverManager.getConnection(DatabaseURL, userName, password);
st = con.createStatement();
rs = st.executeQuery(query);

if(rs.next()){

dbresult= rs.getString(3);
}

}catch (SQLException e) {

e.printStackTrace();
}

return dbresult;

}

Access表由三列组成:用户 ID、用户名、用户密码

最佳答案

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

正如您所提到的,问题在于与数据库本身建立连接。声明

con = DriverManager.getConnection(DatabaseURL, userName, password);

正在抛出被捕获的异常

catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

之后,您将返回已初始化为 nulldbresult(您必须这样做,因为它是一个局部变量)。所以问题是由于身份验证,您无法连接到您的数据库。

在你的机器上(在你的情况下是本地主机)从控制台运行以下命令

GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' IDENTIFIED BY 'root' WITH GRANT OPTION;

关于java.sql.SQLException : Access denied for user 'root' @'localhost' (using password: YES) 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22508922/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com