0

This is this mouseclicked function of table 1 here I am adding data from table 1 to table 2 and also deleting that data from database. And after that calling the clearTable() function for table 1 and again calling the tableData() function for taking fresh values from database

private void tbl_productsMouseClicked(java.awt.event.MouseEvent evt) {                                          
    int i = tbl_products.getSelectedRow();
    model.insertRow(tbl_buy.getRowCount(), new Object[]{
        model1.getValueAt(i, 0),
        model1.getValueAt(i, 1),
        model1.getValueAt(i, 2),
        model1.getValueAt(i, 3),
        model1.getValueAt(i, 4),
        model1.getValueAt(i, 5),
        model1.getValueAt(i, 6),
    });
    sql = "delete from Products where pid = '"+model1.getValueAt(i, 0).toString()+"'";
    try{
        db.st.execute(sql);
    }
    catch(Exception e){
        JOptionPane.showMessageDialog(null, e);
    }
    clearTable1();
    tableData();
}

This is clear tables function

public void clearTable1(){
    while(model1.getRowCount()>0){
        for(int i = 0;i<model1.getRowCount();i++){
            model1.removeRow(i);
        }
    }
}

This is the function that I am using for getting data back from database

private void tableData(){
    try{
        String sql1 = "select * from Products";
        db.rs = db.st.executeQuery(sql1);
        while(db.rs.next()){
            model1.insertRow(tbl_buy.getRowCount(), new Object[]{
                db.rs.getString("pid").toUpperCase(),
                db.rs.getString("pname").toUpperCase(),
                db.rs.getString("pcompany"),
                db.rs.getString("modelNo"),
                db.rs.getString("color"),
                db.rs.getFloat("purch_amt"),
                db.rs.getFloat("sale_amt")
            });
        }
    }
    catch(SQLException e){
        JOptionPane.showMessageDialog(this, e);
    }
}
  • Show the actual exception stacktrace. – Jason Apr 09 '20 at 14:02
  • 3
    Does this answer your question? [What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it?](https://stackoverflow.com/questions/5554734/what-causes-a-java-lang-arrayindexoutofboundsexception-and-how-do-i-prevent-it) – jhamon Apr 09 '20 at 14:02

0 Answers0