Refreshing jTable From database

92 Views Asked by At

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.

0

There are 0 best solutions below