0

I have made a swing application with a jTable. The data is fetched from the database and shown in the table using the following function in the class constructor.

public void Updatetable(){
        try{
            
            String query = "SELECT * FROM stock;";
           pst = connection.prepareStatement(query);
            rs = pst.executeQuery();
            jTable1.setModel(DbUtils.resultSetToTableModel(rs));
        }
        catch (Exception e){
            e.printStackTrace();
        }
    }

This code works fine here. I have then implemented a delete function. It takes the selected row and deletes the corresponding data from the database. It does that perfectly and deletes the data from the database. Now i also need to refresh the table. For that i use the same Updatetable() function but i get the error this time and the program hangs and does not respond. this is the delete method:

 try{
        int row = jTable1.getSelectedRow();
       
       String cell = jTable1.getModel().getValueAt(row, 0).toString();
       String sql = "DELETE from stock WHERE id=" + cell;
      
           pst = connection.prepareStatement(sql);
           pst.execute();
           JOptionPane.showMessageDialog(null, "Deleted successfully");
           
       }
       catch (Exception e){
           JOptionPane.showMessageDialog(null, "An error occured");
       }
       
        
       
        Updatetable(); //here i have called the same function.
       
       
    }                                         

This is the start of the code along with constructor

public class mainpage extends javax.swing.JFrame {



Connection connection;
ResultSet rs;
PreparedStatement pst;

 CardLayout cl;

public mainpage() {
    initComponents();
  
     pack();
    setLocationRelativeTo(null);
    connection = database.Connector();
    if (connection==null) System.exit(1);
    show_product();
    combo();
    combomain();
    editcombo();
}

This is the database class:

public class database {
         public String conn;
    public static Connection Connector(){
        try{
            Class.forName("org.sqlite.JDBC");
            
            Connection conn = DriverManager.getConnection("jdbc:sqlite:users.db");
            return conn;
        } catch (Exception e){
             return null;
        }
       
     }
    
}

The error trace is as follows:

Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 1 >= 1
    at java.util.Vector.elementAt(Vector.java:477)
    at javax.swing.table.DefaultTableModel.getValueAt(DefaultTableModel.java:648)
    at javax.swing.JTable.getValueAt(JTable.java:2720)
    at javax.swing.JTable.prepareRenderer(JTable.java:5712)
    at javax.swing.plaf.synth.SynthTableUI.paintCell(SynthTableUI.java:683)
    at javax.swing.plaf.synth.SynthTableUI.paintCells(SynthTableUI.java:580)
    at javax.swing.plaf.synth.SynthTableUI.paint(SynthTableUI.java:364)
    at javax.swing.plaf.synth.SynthTableUI.update(SynthTableUI.java:275)
    at javax.swing.JComponent.paintComponent(JComponent.java:780)
    at javax.swing.JComponent.paint(JComponent.java:1056)
    at javax.swing.JComponent.paintToOffscreen(JComponent.java:5210)
    at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(RepaintManager.java:1579)
    at javax.swing.RepaintManager$PaintManager.paint(RepaintManager.java:1502)
    at javax.swing.RepaintManager.paint(RepaintManager.java:1272)
    at javax.swing.JComponent._paintImmediately(JComponent.java:5158)
    at javax.swing.JComponent.paintImmediately(JComponent.java:4969)
    at javax.swing.RepaintManager$4.run(RepaintManager.java:831)
    at javax.swing.RepaintManager$4.run(RepaintManager.java:814)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:74)
    at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:814)
    at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:789)
    at javax.swing.RepaintManager.prePaintDirtyRegions(RepaintManager.java:738)
    at javax.swing.RepaintManager.access$1200(RepaintManager.java:64)
    at javax.swing.RepaintManager$ProcessingRunnable.run(RepaintManager.java:1732)
    at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
    at java.awt.EventQueue.access$500(EventQueue.java:97)
    at java.awt.EventQueue$3.run(EventQueue.java:709)
    at java.awt.EventQueue$3.run(EventQueue.java:703)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:74)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:205)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

Any advice on how to handle this would be appreciated.

Ibtsam Ch
  • 383
  • 1
  • 8
  • 22
  • 1
    I think we need to see the implementation of `DbUtils.resultSetToTableModel(...)` method. Could you post it? Even better, you can post a [mre]. – gthanop Aug 12 '20 at 20:20
  • I have used rs2xml.jar libraray. DbUtils is part of that. – Ibtsam Ch Aug 12 '20 at 20:37
  • A ok. Can you post a [mre]? It might help. – gthanop Aug 12 '20 at 20:41
  • i have tried to update the question as you suggested. Moreover this is the import for DbUtils `import net.proteanit.sql.DbUtils;` Kindly check and help. – Ibtsam Ch Aug 12 '20 at 21:07
  • Is this exception always occuring when you run your program? Are you running all Swing related updates on the [EDT](https://docs.oracle.com/javase/tutorial/uiswing/concurrency/dispatch.html)? A [mre] consists of a runnable class providing the code necessary to reproduce the error, but what you have posted so far is not runnable. As per the code already posted, sorry, I don't think I can help you, because I can't think of any other problems with the code posted. – gthanop Aug 12 '20 at 21:20
  • Judging from the stack trace of the exception, seems like the `DbUtils.resultSetToTableModel` method returns a `DefaultTableModel` instance. Why don't you simply use its [`removeRow`](https://docs.oracle.com/javase/8/docs/api/javax/swing/table/DefaultTableModel.html#removeRow-int-) method to clear the corresponding table row when you delete it from the database? You can get the model of the table with [`getModel`](https://docs.oracle.com/javase/8/docs/api/javax/swing/JTable.html#getModel--) and cast it to `DefaultTableModel` as I see it. – gthanop Aug 12 '20 at 21:25

0 Answers0