Search code examples
javamysqljdbc

Statement.execute() returning false even though changes are made


For this piece of my code here:

sql = "DELETE FROM assignments WHERE assign_mark = ?";
PreparedStatement statement = connection.prepareStatement(sql);
statement.setInt(1, d1);

boolean rows = statement.execute();
if (rows == true) {
    new ViewDatabase(user, name, pswrd);
    System.out.println("ASSIGN - ASSIGN_MARK UPDATE SUCCESSFUL!");
    frame.setVisible(false);
} else if (rows == false) {
    JOptionPane.showMessageDialog(null, "Cannot find row!",
              "ERROR", JOptionPane.ERROR_MESSAGE);
}

statement.close();
connection.close();

After I enter the value of d1 (which corresponds to the correct column of an existing data in my database), I get the "Cannot find row!" value. However, when I check my database, said row with d1 in column name is gone. I'm not sure what error/misunderstanding of concept I have here.


Solution

  • int numColsAffected = statement.executeUpdate();
    if (numColsAffected > 0) {
       // Success
    }
    else {
       // Failure
    }
    

    is probably the way to go