0

I am USING mysql (XAMPP) on my netbeans.

I am trying the query

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

but cant't connect:

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

Here the whole code:

    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();
      }
      }
user1981275
  • 13,002
  • 8
  • 72
  • 101
Ciarra Borja
  • 3
  • 1
  • 3
  • possible duplicate of [Access denied for user 'root'@'localhost' (using password: YES) (Mysql::Error)](http://stackoverflow.com/questions/6081339/access-denied-for-user-rootlocalhost-using-password-yes-mysqlerror) – Nir Alfasi Mar 10 '15 at 01:35
  • I tried it but there is still an error, it.. Are my codes correct(GUI) . ? What should i do.. – Ciarra Borja Mar 10 '15 at 01:51
  • When i run this code, the frame appear, then when i click the nexthandler button to save it on my database, this error appears .. – Ciarra Borja Mar 10 '15 at 01:53
  • is your password really a whitespace? String password= " "; – Cedric Mar 10 '15 at 03:35

2 Answers2

0

After you run your query you have to run

FLUSH PRIVILEGES

Your code is taking a space as password. Please change password =" " to password=""

Leandro Papasidero
  • 3,728
  • 1
  • 18
  • 33
0

The problem is with your connection string. String password = " ";

Change it to String password = "root";

(If still there is error please try this in command prompt and see what happens.
mysql -u root -p (press enter)
password : root

Krish Nakum R
  • 505
  • 1
  • 8
  • 19