【问题标题】:Error:java.sql.SQLException: Access denied for user 'root@localhost'@'localhost' (using password: YES)错误:java.sql.SQLException:用户'root@localhost'@'localhost'的访问被拒绝(使用密码:是)
【发布时间】:2015-05-11 08:39:42
【问题描述】:

我在我的 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();
      }
      }

【问题讨论】:

  • 我试过了,但仍然有错误,它.. 我的代码是否正确(GUI)。 ?我该怎么办..
  • 当我运行此代码时,会出现框架,然后当我单击 nexthandler 按钮将其保存在我的数据库中时,会出现此错误..
  • 您的密码真的是空格吗?字符串密码= " ";

标签: java mysql database netbeans xampp


【解决方案1】:

运行查询后,您必须运行

FLUSH PRIVILEGES

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

【讨论】:

    【解决方案2】:

    问题在于您的连接字符串。 字符串密码 = " ";

    将其更改为 String password = "root";

    (如果仍然有错误,请在命令提示符下尝试此操作,看看会发生什么。
    mysql -u root -p(按回车键)
    密码:root

    【讨论】:

      猜你喜欢
      • 2014-09-10
      • 2012-08-08
      • 1970-01-01
      • 2020-03-18
      • 2012-08-11
      • 2021-08-05
      • 2018-04-24
      相关资源
      最近更新 更多