0

enter image description hereenter image description here I want to delete a record from database. The code runs fine, but even when there is a record for a particular id, the message returns id not found in database. Please help me to improve my code to display a separate message when id is not found in database.i have attached my output. as u can see id=19 i there in database but when i delete id=19, that particular id record gets deleted from the database but message pops up saying id not found in database.

try {
                                    String s = TUID.getText( );
                                    Connection con = DriverManager.getConnection( "jdbc:mysql://localhost:3306/mysql", "root", "root" );
                                    Statement st = con.createStatement( );
                                    String r = "delete from mysql57.addressbook where iid = "+s;
                                    PreparedStatement preparedStatement = con.prepareStatement( r );
                                    preparedStatement.executeUpdate( );
                                    int rows = preparedStatement.executeUpdate();
                                 if(rows == 0)
                        {  JOptionPane.showMessageDialog( f, "ID NOT FOUND IN DATABASE");



                        }

                        else{
                            JOptionPane.showMessageDialog( f, "DELETED FROM DATABASE" );

                        }

                                    }


                                }
                                catch (Exception ex) {
                                    JOptionPane.showMessageDialog( f,ex );
                                }
Jyoti
  • 103
  • 1
  • 11

1 Answers1

2

The executeUpdate() method returns the amount of rows affected. If no rows were deleted, it returns 0 (naturally).

So...

int rows = preparedStatement.executeUpdate();
if(rows == 0)
    ...
else
    ...

You should also use PreparedStatement correctly, and change that + s into a placeholder (it's amazing how every single tutorial seems to abuse PreparedStatement and teach bad habits).

Kayaman
  • 72,141
  • 5
  • 83
  • 121
  • 2
    Some bedtime reading: https://stackoverflow.com/questions/3271249/difference-between-statement-and-preparedstatement – Kayaman Nov 15 '17 at 20:23
  • String r = "delete from mysql57.addressbook where iid = " + s; PreparedStatement preparedStatement = con.prepareStatement( r ); preparedStatement.executeUpdate( ); int rows = preparedStatement.executeUpdate(); if(rows == 0){ JOptionPane.showMessageDialog( f, "ID NOT FOUND IN DATABASE" ); } else{ JOptionPane.showMessageDialog( f, "DELETED FROM DATABASE" ); } FOR BOTH ID MATCHED AN NOT MATCHED . I AM GETTING AN MESSAGE "ID NOT FOUND IN DATABASE". – Jyoti Nov 15 '17 at 22:14
  • Then the id doesn't match. – Kayaman Nov 16 '17 at 06:17
  • When id is matching the same message pops up showing ID not found – Jyoti Nov 16 '17 at 06:37
  • Edit the question and include the code in there, you can see how illegible code is in comments. – Kayaman Nov 16 '17 at 06:48
  • If `rows == 0`, then nothing was deleted, meaning your delete statement didn't match anything in the database. – Kayaman Nov 16 '17 at 07:09
  • yes you are right but if the id matches then else block should be executed and message "id deleted from database '' should be shown,but this is not happening. – Jyoti Nov 16 '17 at 07:17
  • It's not happening because the id doesn't match. If the id did match, then you would get the "id deleted" message. This is a **fact**. It doesn't matter if you say that the id matches, the code says that it doesn't match. – Kayaman Nov 16 '17 at 07:23
  • that particular id record gets deleted from the database which means the code says that it does match but message displayed is wrong. – Jyoti Nov 16 '17 at 07:32
  • i found my mistake. – Jyoti Nov 16 '17 at 07:34