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:
}
NOT ADMINreally an actual privilege? What database are you using?DELETE FROM user WHERE privilege = 'NOT ADMIN' AND username = 'lec'... use this syntax, do not use a plus symbolPreparedStatementto avoid an SQL injection attack. This is just a learning exercise, but better to get into good habits now.