Search code examples
javaoracle-databaseswingif-statementjoptionpane

IF ELSE statement in java delete sql joption message


i tried to create a if statement but don't work, if found isbn book in jtable, show message "book" delete" else "book" don't found. it's possible?

 private void elimina_libroActionPerformed(java.awt.event.ActionEvent evt) {      
   Connection conn = Connessione.ConnecrDb();
   Statement stmt = null;
   ResultSet emps = null;       
      boolean found = false;
    try{          
      if(emps.next()) 
      found= true;
        String sql= "DELETE FROM progetto.libro WHERE isbn =?";  
        pst=(OraclePreparedStatement) conn.prepareStatement(sql);            
        pst.setString (1, txt_isbn.getText());          
        pst.execute();               

    if (!found)
       JOptionPane.showMessageDialog(null, "LIBRO ELIMINATO"); 
   else{    
            JOptionPane.showMessageDialog(null, "BOOK NOT FOUND","ERRORE",jOptionPane.WARNING_MESSAGE); 

    }

    }
    catch (Exception e)
     {
      JOptionPane.showMessageDialog(null,e);
                }    

but don't work, can you help me? thanks

i change code with this

Connection conn = Connessione.ConnecrDb();
   Statement stmt = null;
   ResultSet emps = null;

    try{

        String sql= "DELETE FROM progetto.libro WHERE isbn =?"; 

        pst=(OraclePreparedStatement) (PreparedStatement) conn.prepareStatement(sql);            
          pst.setString (1, txt_isbn.getText());          
        int deleted = pst.executeUpdate();               

if (deleted == 0) {

         JOptionPane.showMessageDialog(null, "LIBRO ELIMINATO");
         Update_table(); 


          }else
               JOptionPane.showMessageDialog(null, "LIBRO NON TROVATO");  

    }

    catch (Exception e)
     {
      JOptionPane.showMessageDialog(null,e);

    }    

but if i insert a incorrect isbn the message is: "book delete", if i insert a correct isbn the message is "book not found" and if i insert again correct isbn the message is "book delete" and the book delete from databae.


Solution

  •     pst=(PreparedStatement) conn.prepareStatement(sql);            
        pst.setString (1, txt_isbn.getText());          
        int deleted = pst.executeUpdate();               
    
        if (deleted == 0) {
        ...
    

    int executeUpdate() throws SQLException

    Executes the SQL statement in this PreparedStatement object, which must be an SQL Data Manipulation Language (DML) statement, such as INSERT, UPDATE or DELETE; or an SQL statement that returns nothing, such as a DDL statement.

    Returns: either (1) the row count for SQL Data Manipulation Language (DML) statements or (2) 0 for SQL statements that return nothing