英文:
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 = "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);
}
}
专注分享java语言的经验与见解,让所有开发者获益!
评论