gpt4 book ai didi

java - 错误 :java. sql.SQLException:用户 'root@localhost' @'localhost' 的访问被拒绝(使用密码:YES)

转载 作者:行者123 更新时间:2023-11-29 22:49:35 24 4
gpt4 key购买 nike

我在我的 netbeans 上使用 mysql (XAMPP)。

我正在尝试查询

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

但无法连接:

  ERROR: java.sql.SQLException:Access denied for user 'root'@'localhost'

这里是整个代码:

    public class NextHandler implements ActionListener{
@Override
public void actionPerformed (java.awt.event.ActionEvent evt){
try{

String url="jdbc:mysql://localhost:3306/database2";
String username= "root";
String password= " ";
Connection con=DriverManager.getConnection(url, username,password);
Statement stat=con.createStatement();
String Query=
"INSERT INTO donor_info(donor_id,donor_lastname,donor_firstname,donor_middlename,donor_age,donor_weight,donor_height,donor_gender,donor_contact_number,"
+ "medical_condition)VALUES ('"+txtdonorid.getText()+"','"+txtlname.getText()+"','"+txtfname.getText()+"','"+txtmname.getText()+"',"
+ "'"+txtage.getText()+"','"+txtweight.getText()+"','"+txtheight.getText()+"')";

stat.execute(Query);
JOptionPane.showMessageDialog(null,"Donor Added to Database");
txtdonorid.setText(null);
txtlname.setText(null);
txtfname.setText(null);
txtmname.setText(null);
txtage.setText(null);
txtweight.setText(null);
txtheight.setText(null);

}
catch (SQLException ex){
JOptionPane.showMessageDialog(null,ex.toString());
}
bloodtype f=new bloodtype();
f.setVisible(true);
dispose();
f.run();
}
}


public void run(){

//setUndecorated(true);

setSize(1920, 1200);
setLocationRelativeTo(null);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

public static void main(String[] args) throws Exception{
Class.forName("com.mysql.jdbc.Driver");
donorinfo f = new donorinfo();
f.run();
}
}

最佳答案

运行查询后,您必须运行

FLUSH PRIVILEGES

您的代码使用空格作为密码。请将 password="" 更改为 password=""

关于java - 错误 :java. sql.SQLException:用户 'root@localhost' @'localhost' 的访问被拒绝(使用密码:YES),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28954740/

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