简体   繁体   English

如何将jtable单元格值编辑/更新到数据库中

[英]how to edit/update jtable cell values into database

am trying to edit and update jtable cells as in my code below. 我正在尝试按照下面的代码编辑和更新jtable单元格。 my problem is that a single row when updated all the other rows get the same values. 我的问题是,更新后的所有其他行中的一行都具有相同的值。 i mean only one row is updated and all other a duplicated. 我的意思是只有一行被更新,其他所有重复。 can any one help with a good approach. 任何人都能以一种好的方法提供帮助。 thanks 谢谢

    int count = Table_purchase.getRowCount();
    int col = Table_purchase.getColumnCount();
    String pod_id[] = new String[count];
    String po_id[] = new String[count];
    String order_qty[] = new String[count];
    String item_id[] = new String[count];
    String unit_price[] = new String[count];
    String recived_qty[] = new String[count];
    String rejected_qty[] = new String[count];

    for (int i = 0; i < count; i++) {
        po_id[i] = Table_purchase.getValueAt(i,0).toString();
        pod_id[i] = Table_purchase.getValueAt(i,1).toString();
        order_qty[i] = Table_purchase.getValueAt(i,2).toString();
        item_id[i] = Table_purchase.getValueAt(i,3).toString();
        unit_price[i] = Table_purchase.getValueAt(i,4).toString();
        recived_qty[i] = Table_purchase.getValueAt(i, 5).toString();
        rejected_qty[i] = Table_purchase.getValueAt(i,6).toString();

        try {
            String sql = "update purchase.purchase_detail set pod_id='" + pod_id[i] + "',order_qty='" + order_qty[i] + "',item_id='" + item_id[i] + "', unit_price='" + unit_price[i] + "', recived_qty='" + recived_qty[i] + "',rejected_qty='" + rejected_qty[i] + "'where  po_id= '" +  po_id[i] + "'";
            pst = conn.prepareStatement(sql);
            pst.execute();
            JOptionPane.showMessageDialog(null, "updated");
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, e);
        }  

    }

Your sql statement does not contain a where-clause, hence all rows in the database table are updated for each iteration of the swing-table-rows, and in the end, all the database-rows will have the values from the last swing-table-row. 您的sql语句不包含where子句,因此数据库表中的所有行都会针对swing-table-rows的每次迭代进行更新,最后,所有数据库行都将具有上一个swing-的值。表行。

(And, use pst.setParameter ( http://docs.oracle.com/javase/tutorial/jdbc/basics/prepared.html ) and do not mix sql into gui-code.) (并且,请使用pst.setParameter( http://docs.oracle.com/javase/tutorial/jdbc/basics/prepared.html ),并且不要将sql混入gui代码中。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM