首页 > 解决方案 > 从 jTable java.lang.ArrayIndexOutOfBoundsException: 1 > 0 中删除行时出现此错误

问题描述

这是表 1 的鼠标单击功能,这里我将表 1 中的数据添加到表 2,并从数据库中删除该数据。然后为表 1 调用 clearTable() 函数并再次调用 tableData() 函数以从数据库中获取新值

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();
}

这是清除表格功能

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

这是我用来从数据库取回数据的函数

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);
    }
}

标签: javaexceptionindexoutofboundsexception

解决方案


推荐阅读