2017-08-25 40 views
5

我無法通過編輯/更改單元格值並單擊更新按鈕來更新數據庫中的數據,但他無法更改我更改爲單元格的數據庫。它仍然是最後一個值。java -update數據庫通過編輯單元格不工作?

這裏是我的代碼:

public void directlyup() { 
     int col=tablesample.getSelectedColumn(); 
     int row=tablesample.getSelectedRow(); 
     int index; 
     index = Integer.parseInt(tablesample.getModel().getValueAt(row, 0).toString()); 
     try { 
      String sql ="UPDATE invoice SET description = ?,qty = ?,unitprice = ?" 
        + ",total = ? WHERE id ="+row; 
      pst = conn.prepareStatement(sql); 
      pst.setString(1, (String) tablesample.getValueAt(row, 1)); 
      pst.setString(2, (String) tablesample.getValueAt(row, 2)); 
      pst.setString(3, (String) tablesample.getValueAt(row, 3)); 
      pst.setString(4, (String) tablesample.getValueAt(row, 4)); 
      pst.execute(); 
      JOptionPane.showMessageDialog(null, "Successfully Updated"); 
     } catch (Exception e) { 
     JOptionPane.showMessageDialog(null, e); 
     } 
    } 
+3

你的問題是什麼? –

+1

歡迎來到SO。所提供的信息不足以幫助您。嘗試打印'index'和'(String)tablesample.getValueAt(row,X)'。你可能會感到驚訝。 – c0der

+0

我已經試着把索引:String sql =「更新發票SET描述=?,數量=?,單位價格=?」 +「,total =?WHERE id =」+ index;但唯一可以更新的是第一行,其他將與第一行相同 –

回答

6
String sql ="UPDATE invoice SET description = ?,qty = ?,unitprice = ?" 
       + ",total = ? WHERE id ="+row; 

你爲什麼試圖嵌入的SQL變量作爲where子句?

只需使用類似於您的參數就可以實現其他值。它會保持SQL更簡單:

String sql = 
    "UPDATE invoice SET description = ?, qty = ?, unitprice = ?, total = ? WHERE id = ?"; 
... 
pst.set???(5, row); 
+0

感謝它真的可以更新我在jtable中更改的特定行,但是如果我想更新所有更改的行,該怎麼辦? –

+0

你是什麼意思?您一次只能更改一行,因此每次更改時都會調用代碼。 – camickr

1

這可能是你打算在WHERE子句中使用索引而不是行?

String sql ="UPDATE invoice SET description = ?,qty = ?,unitprice = ?" 
       + ",total = ? WHERE id ="+index; 
1

假設你已經多行選擇,如果你的ID爲int:

int[]rows=tablesample.getSelectedRows(); 


String sql="UPDATE invoice SET description = ?,qty = ?,unitprice = ?" 
     + ",total = ? WHERE id =?"; 

try{ 
    PreparedStatement pst = conn.prepareStatement(sql); 
    for(int currentRow:rows){ 


       pst.setString(1, (String) tablesample.getValueAt(currentRow, 1)); 
       pst.setString(2, (String) tablesample.getValueAt(currentRow, 2)); 
       pst.setString(3, (String) tablesample.getValueAt(currentRow, 3)); 
       pst.setString(4, (String) tablesample.getValueAt(currentRow, 4)); 
       pst.setInt(5, currentRow); 
       pst.executeUpdate(); 



     } 
    JOptionPane.showMessageDialog(null, "Successfully Updated"); 
    } catch (Exception e) { 
     JOptionPane.showMessageDialog(null, e); 
    } finally{ 
     try { 
      conn.close(); 
     } catch (SQLException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
    } 

獲得最佳的性能,您可以使用則ExecuteBatch這樣example

2

對於CRUD操作上的一個數據集,很高興使用中間表。這避免了大量的查詢被放置在大型數據集上。

給我的建議,以解決這個問題之前,我想指出,對數據庫結構的一些言論,我有:

  1. total領域顯然是一個計算的字段。這些信息並不適合放入數據庫。這是根據要求計算的。
  2. 整套數據顯然是文件(發票)的一部分。因此,數據庫中必須有一個字段,用於唯一標識與數據相關的文檔。

另外,我想說的是,這樣的決定是爲特定的數據庫做出的。在這種情況下,我的解決方案涉及mysql

這是在其底部的代碼段運行

CREATE TABLE `invoice` (
    `id` int(10) unsigned NOT NULL AUTO_INCREMENT, 
    `invoice_id` int(10) unsigned NOT NULL, 
    `description` varchar(255) DEFAULT NULL, 
    `qty` double DEFAULT NULL, 
    `unitprice` double DEFAULT NULL, 
    PRIMARY KEY (`id`) 
) ENGINE=InnoDB 

表的DDL而這是使上使用文檔ID密鑰(invoce_id)的數據集CRUD操作的代碼。

public boolean save(long invoice_id, List<Invoice> list) throws SQLException { 
    try(Connection connection = getConnection()) { 
     try { 
      connection.setAutoCommit(false); 

      String query = 
        "create temporary table if not exists `invoice_tmp` (" + 
          "`id` int(10) unsigned NOT NULL," + 
          "`description` varchar(255) DEFAULT NULL," + 
          "`qty` double DEFAULT NULL," + 
          "`unitprice` double DEFAULT NULL)"; 

      connection.createStatement().executeUpdate(query); 

      query = "insert into `invoice_tmp` values (?, ?, ?, ?)"; 
      PreparedStatement statement = connection.prepareStatement(query); 
      for(Invoice invoice: list) { 
       statement.setLong(1, invoice.getId()); 
       statement.setString(2, invoice.getDescription()); 
       statement.setDouble(3, invoice.getQty()); 
       statement.setDouble(4, invoice.getUnitPrice()); 

       statement.addBatch(); 
      } 

      statement.executeBatch(); 
      statement.close(); 

      query = 
        "delete invoice from invoice " + 
        "left join invoice_tmp on (invoice.id = invoice_tmp.id) " + 
        "where invoice_id = ? and invoice_tmp.id is null"; 

      statement = connection.prepareStatement(query); 
      statement.setLong(1, invoice_id); 
      statement.executeUpdate(); 
      statement.close(); 

      query = 
        "update `invoice` " + 
        "join `invoice_tmp` using (`id`) " + 
        "set " + 
          "`invoice`.description = `invoice_tmp`.description, " + 
          "`invoice`.qty = `invoice_tmp`.qty, " + 
          "`invoice`.unitprice = `invoice_tmp`.unitprice"; 

      connection.createStatement().executeUpdate(query); 

      query = 
        "insert into `invoice` (`invoice_id`, `description`, `qty`, `unitprice`) " + 
        "select ? as `invoice_id`, `description`, `qty`, `unitprice` from `invoice_tmp` where `id` = 0"; 

      statement = connection.prepareStatement(query); 
      statement.setLong(1, invoice_id); 
      statement.executeUpdate(); 
      statement.close(); 

      connection.createStatement().executeUpdate("drop table if exists `invoice_tmp`"); 

      connection.commit(); 
      return true; 
     } 
     catch (Exception e) { 
      connection.rollback(); 
      throw e; 
     } 
    } 
} 

this是一個測試項目,演示了上述代碼如何工作。

相關問題