2016-03-19 17 views
0

以下のコードのようにjtableセルを編集して更新しようとしています。私の問題は、他のすべての行が更新されたときに単一の行が同じ値を取得するということです。私は1つの行だけが更新され、他のすべてが複製されていることを意味します。いずれかが良いアプローチで助けることができます。おかげデータベースにjtableセルの値を編集/更新する方法

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); 
     } 

    } 

答えて

0

WHERE句、したがって、データベーステーブルのすべての行は、スイング・テーブルの行の反復ごとに更新され、最終的にされているが含まれていないSQL文は、すべてのデータベース行う最後のswing-table-rowの値を持つ。

(また、pst.setParameter(http://docs.oracle.com/javase/tutorial/jdbc/basics/prepared.html)を使用し、sqlをgui-codeに混ぜて使用しないでください)

関連する問題