0

I am trying to delete a record in database where if the username i typed is not admin i can delete it however its not working I think its the query any idea thanks in advance.I am a newbie i been trying to figure it out ...

   String password = new String(oldPass.getPassword());
    String realpass = pw.getText();
    String us = userr.getText();
      user = us;

        System.out.println("ok");
        String query = "DELETE FROM user WHERE privilege = 'NOT ADMIN' +        username = '"+us+"'";
      try {
            Statement st = (Statement) con.createStatement();
            int r = st.executeUpdate(query);
            if (r != 0) {
                JOptionPane.showMessageDialog(null, "Successfully deleted!", "Delete", JOptionPane.OK_OPTION);

                login w = new login();
                w.setVisible(true);
                this.dispose();
            } else {
                JOptionPane.showMessageDialog(null, "Wait! something's wrong, please try again later.", "Ooopppss!", JOptionPane.OK_OPTION);


            }

        } catch (Exception e) {
            System.out.println(e);
        }


      // TODO add        // TODO add your handling code here:
}                              
4
  • 2
    Your query looks wrong in so many ways. It has a plus symbol in it, and is NOT ADMIN really an actual privilege? What database are you using? Commented Dec 6, 2016 at 0:45
  • im using php mysql even if i change the privilege as staff its still not deleted Commented Dec 6, 2016 at 0:49
  • DELETE FROM user WHERE privilege = 'NOT ADMIN' AND username = 'lec' ... use this syntax, do not use a plus symbol Commented Dec 6, 2016 at 0:50
  • 1
    I'd strongly recommend using a PreparedStatement to avoid an SQL injection attack. This is just a learning exercise, but better to get into good habits now. Commented Dec 6, 2016 at 2:23

1 Answer 1

1

Maybe you are missing the AND or the Or operator depend on what are you trying to do here an example

DELETE FROM user WHERE privilege = 'NOT ADMIN' AND user='user'

Anyway I recommend you that instead use the string 'not admin' you create a new catalogue from privileges

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.