I am getting this error while removing rows from jTable java.lang.ArrayIndexOutOfBoundsException: 1 > 0

huangapple 未分类评论52阅读模式
英文:

I am getting this error while removing rows from jTable java.lang.ArrayIndexOutOfBoundsException: 1 > 0

问题

以下是翻译好的部分:

这是鼠标点击函数,用于表格1。在这里,我将来自表格1的数据添加到表格2,并从数据库中删除该数据。之后,调用clearTable()函数来清除表格1中的数据,并再次调用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);
    }
}
英文:

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 = &quot;delete from Products where pid = &#39;&quot;+model1.getValueAt(i, 0).toString()+&quot;&#39;&quot;;
    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()&gt;0){
        for(int i = 0;i&lt;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 = &quot;select * from Products&quot;;
        db.rs = db.st.executeQuery(sql1);
        while(db.rs.next()){
            model1.insertRow(tbl_buy.getRowCount(), new Object[]{
                db.rs.getString(&quot;pid&quot;).toUpperCase(),
                db.rs.getString(&quot;pname&quot;).toUpperCase(),
                db.rs.getString(&quot;pcompany&quot;),
                db.rs.getString(&quot;modelNo&quot;),
                db.rs.getString(&quot;color&quot;),
                db.rs.getFloat(&quot;purch_amt&quot;),
                db.rs.getFloat(&quot;sale_amt&quot;)
            });
        }
    }
    catch(SQLException e){
        JOptionPane.showMessageDialog(this, e);
    }
}

huangapple
  • 本文由 发表于 2020年4月9日 21:59:06
  • 转载请务必保留本文链接:https://java.coder-hub.com/61122877.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定