2016-03-31 24 views
0

Jtable'dan her satır için bir veritabanına eklemek istiyorum. Örneğin. İlk ürünü masaya ekler ama her birini eklemesini istiyorum.JTable'da her satır için veri tabanına ekle

private void addToInvoiceLine(){ 
try { 
     String sql = "Insert Into invoiceLine (invoiceID,SKU,quantity) values (?,?,?)"; 
     pst = conn.prepareStatement(sql); 
     int row = resultsTable.getSelectedRow(); 

     String sku = (orderTable.getModel().getValueAt(row, 0).toString()); 
     String quantity = (orderTable.getModel().getValueAt(row, 2).toString()); 
     pst.setString(1, invoiceNo.getText()); 
     pst.setString(2, sku); 
     pst.setString(3, quantity); 

     pst.execute(); 


    } catch (SQLException | HeadlessException e) { 

     JOptionPane.showMessageDialog(null, e); 
    } finally { 
     try { 
      rs.close(); 
      pst.close(); 
     } catch (Exception e) { 
     } 
    } 

} 
+0

Eğer veritabanı tablosuna 'Jtable' tüm satırları kopyalamak istiyor musunuz? –

+0

Evet, jTable – ZeeSoft

+0

ürününe hiç bir ürün eklenmemiş. Ve bir döngü kullanırken neyin yanlış olduğu? (int satır = 0; satır AJNeufeld

cevap

0

Bir döngü yapmak gerektiğini böyle tablodaki her satırın üzerinde yinelenir:

private void addToInvoiceLine(){ 
    try { 
     String sql = "INSERT INTO invoiceLine (invoiceID,SKU,quantity) VALUES (?,?,?)"; 
     pst = conn.prepareStatement(sql); 


     for(int row=0; row<orderTable.getModel().getRowCount(); row++){ 
      String sku = orderTable.getModel().getValueAt(row, 0).toString(); 
      String quantity = orderTable.getModel().getValueAt(row, 2).toString(); 

      pst.setString(1, invoiceNo.getText()); 
      pst.setString(2, sku); 
      pst.setString(3, quantity); 

      pst.execute(); 
     } 


    } catch (SQLException | HeadlessException e) { 
     JOptionPane.showMessageDialog(null, e); 
    } finally { 
     try { 
      rs.close(); 
      pst.close(); 
     } catch (Exception e) { 
     } 
    } 

}