***How To UpDate JTable on Delete Command and if given id don't match with database then a message show "id is not available "

My Codes as follows:-***

private void Sf_Reg_delete_ButtonActionPerformed(java.awt.event.ActionEvent evt) {                                                     
        String str2 = JOptionPane.showInputDialog(null, "Please Note that this Reg No is not generated again : ","Enter Reg No For Delete", 1);
        try
        {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            con=DriverManager.getConnection("jdbc:odbc:HMS_DS","","");
                    sql="DELETE from Staff_Reg WHERE sf_Reg_No = '"+str2+"'";
                    stmt=con.createStatement();
                    int delete = stmt.executeUpdate(sql);
                    if(str2.equals(""))
                    {
                        JOptionPane.showMessageDialog(null,"Please Enter Reg No.");
                    }
                    else
                    {
                        if(delete == 1)
                        {
                            JOptionPane.showMessageDialog(null,"Record Is Deleted.");

                        }
                        else
                        {
                            JOptionPane.showMessageDialog(null,"Reg No Not Found..");
                        }
                    }  
                    UpdateData_Table();
            }
       catch(Exception ex)
       {
           //JOptionPane.showMessageDialog(null,ex);
           JOptionPane.showMessageDialog(null,"Record Can Not Be Deleted.");
       }
    }        

what table model are you using? some table models do not allow deletion. What are you using?

You set DefaultTableModel to your JTable in one method like setTableModel() and call that setTableModel() method to in your code where you need to update a table contents.

Comments
Bad advice.

can you give me an example .
i am a student so don't know so much about that.

Implement your own AbstractTableModel and let it interact with the database.
You'll have to provide your own remove method there, and make use of the fireTableRowsDeleted method to notify the JTable that a row was deleted.

Here are two links that should provide you with the information you need (including examples):

http://docs.oracle.com/javase/tutorial/uiswing/components/table.html
http://www.informit.com/articles/article.aspx?p=332278

Edited 3 Years Ago by mvmalderen

This article has been dead for over six months. Start a new discussion instead.