java - 从 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);
}
}
解决方案
推荐阅读
- java - 使用 Spring Reactor 从关系数据库中检索数据
- python - 在 Pandas 中 groupby 之后将 Series 索引转换为 df 列
- r - R最小化具有梯度的投资组合函数
- java - Java PriorityQueue.remove() 时间复杂度?O(log N) 还是 O(N)?
- python - 熊猫添加新列性能问题
- python - 从 numpy 数组中提取数据以创建另一个数组
- python - 在 Python 中创建一个包含一系列数字的矩阵
- angular - 如何在ionic4中为电容器设置本地存储
- java - 如何从 HashMap 中的 HashMap 获取 Set
- javascript - matter.js 空白屏幕,没有加载