0
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
   String del_user = JOptionPane.showInputDialog(null,"Enter the Username To be deleted     :"  ,"Delete User !", JOptionPane.WARNING_MESSAGE);

   try {    
      stmt = con.createStatement(); 
      String quer = "delete from Udet where username='"+del_user+"'";
      rs=stmt.executeQuery(quer);

      int count=0; 
      while(rs.next())
      {
         count++;
     }

     if(count==1)
     {
        JOptionPane.showMessageDialog(null,"The user has been deleted");
     }

     else
     {
        JOptionPane.showMessageDialog(null,"No such User Exists");
     }

  } catch(Exception e){}


}        

THE QUERY EXECUTES FINE ! THE RECORD GETS DELETED BUT THE LINES AFTER QUERYEXECUTION ARE NOT EXECUTING ... the JOptionPane will work after the try block but then the value of count wont be determined...

M A
  • 71,713
  • 13
  • 134
  • 174
devcodes
  • 1,038
  • 19
  • 38

1 Answers1

0

A DELETE statement doesn't return a ResultSet and therefore you should use the method stmt.executeUpdate(quer); instead of stmt.executeQuery(quer);. One way you can see how many results were deleted from the query is run a SELECT COUNT(*) query on the table before and after the DELETE using the executeQuery() method which will both times return a ResultSet with a single result containing how many records the table contains. After running it before and after the DELETE you can compare the 2 numbers. The result will probably be a String but you can just use int integer = Integer.parseInt(String); to get the int from it. The code should look like this:

stmt = "SELECT COUNT(*) AS Count FROM table_name";
int initialSize = 0;
rs = stmt.executeQuery(stmt);
while(rs.next){
  initialSize = Integer.parseInt(rs.getString("Count");
}

Run the DELETE query and then repeat the COUNT query passing the int into a new variable (for this answer I'll call it newSize) so the number of changes the DELETE made would be:

int changesMade = initialSize - newSize;
cbender
  • 2,231
  • 1
  • 14
  • 18