数据库更新产品的数量,如果java中的代码是jtable
我已经为商店创建了一个pos系统。销售产品时,所有属性都存储在jtable中,然后插入到数据库中。如果产品多次出现,我想更新/增加产品数量
我的代码如下。但它并不奏效。它正在更新同一产品的数量,但如果出现不同的产品,则不会在表中添加超过1行
DefaultTableModel model = (DefaultTableModel) sale_table.getModel();
int numofRows = sale_table.getRowCount();
if (numofRows == 0) {
Vector v = new Vector();
v.add(s_pname.getText());
v.add(s_bcode.getText());
v.add(s_mar_price.getText());
v.add(our_price.getText());
v.add(s_qty.getValue().toString());
v.add(s_t_discount.getText());
v.add(s_t_price.getText());
v.add(t_profit.getText());
model.addRow(v);
sale_cart_tot();
store_profit();
// Calculate the Balance amount
Double paid = 0.0;
Double tot = Double.valueOf(cart_total.getText());
Double due;
due = paid - tot;
cart_bal.setText(String.valueOf(due));
s_bcode.setText("");
s_pname.setText("");
s_mar_price.setText("");
s_t_price.setText("");
our_price.setText("");
s_t_discount.setText("");
s_bcode.requestFocus();
s_qty.setValue(0b1);
}
else if (numofRows != 0) {
for (int i = 0; i <= numofRows; i++) {
String tb_bcode = sale_table.getValueAt(i, 1).toString();
while (bcode.equals(tb_bcode)) {
int tb_qty = Integer.parseInt(sale_table.getValueAt(i, 4).toString());
double tb_dis = Double.valueOf(s_t_discount.getText());
double tb_tprice = Double.valueOf(s_t_price.getText());
double tb_profit = Double.valueOf(u_profit.getText());
int update_qty = tb_qty + 1;
double update_dis = update_qty * tb_dis;
double update_tprice = update_qty * tb_tprice;
double update_profit = update_qty * tb_profit;
sale_table.setValueAt(update_qty, i, 4);
sale_table.setValueAt(update_dis, i, 5);
sale_table.setValueAt(update_tprice, i, 6);
sale_table.setValueAt(update_profit, i, 7);
sale_cart_tot();
store_profit();
Double paid = 0.0;
Double tot = Double.valueOf(cart_total.getText());
Double due;
due = paid - tot;
cart_bal.setText(String.valueOf(due));
s_bcode.setText("");
s_pname.setText("");
s_mar_price.setText("");
s_t_price.setText("");
our_price.setText("");
s_t_discount.setText("");
s_bcode.requestFocus();
s_qty.setValue(0b1);
}
}
} else if(numofRows != 0){
Vector v = new Vector();
v.add(s_pname.getText());
v.add(s_bcode.getText());
v.add(s_mar_price.getText());
v.add(our_price.getText());
v.add(s_qty.getValue().toString());
v.add(s_t_discount.getText());
v.add(s_t_price.getText());
v.add(t_profit.getText());
model.addRow(v);
sale_cart_tot();
store_profit();
Double paid = 0.0;
Double tot = Double.valueOf(cart_total.getText());
Double due;
due = paid - tot;
cart_bal.setText(String.valueOf(due));
s_bcode.setText("");
s_pname.setText("");
s_mar_price.setText("");
s_t_price.setText("");
our_price.setText("");
s_t_discount.setText("");
s_bcode.requestFocus();
s_qty.setValue(0b1);
JOptionPane.showMessageDialog(null, "Different");
}
谁能解决我的问题
共 (0) 个答案