2017-08-25 24 views
5

Tôi đang gặp sự cố khi cập nhật dữ liệu từ cơ sở dữ liệu bằng cách chỉnh sửa/thay đổi giá trị ô và nhấp vào nút cập nhật nhưng không thể thay đổi cơ sở dữ liệu mà tôi đã thay đổi thành ô. nó vẫn như cũ vào giá trị cuối cùng.cơ sở dữ liệu java-update bằng cách chỉnh sửa các ô không hoạt động?

đây là mã của tôi:

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

Câu hỏi của bạn là gì? –

+1

Chào mừng bạn đến với SO. Thông tin được cung cấp không đủ để giúp bạn. Thử in ra 'index' và' (String) tablesample.getValueAt (hàng, X) '. Bạn có thể ngạc nhiên. – c0der

+0

Tôi đã cố gắng để đưa chỉ mục: String sql = "UPDATE hóa đơn SET mô tả =?, Qty =?, Unitprice =?" + ", total =? WHERE id =" + chỉ mục; nhưng chỉ có thể cập nhật là hàng đầu tiên và những người khác sẽ giống như hàng đầu tiên –

Trả lời

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

Tại sao các bạn cố gắng để nhúng một biến trong SQL cho mệnh đề ở đâu?

Chỉ cần sử dụng thông số như bạn thực hiện cho các giá trị khác. Nó sẽ giữ cho SQL đơn giản hơn:

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

cảm ơn nó thực sự hoạt động để cập nhật một hàng cụ thể tôi thay đổi trong jtable, nhưng nếu tôi muốn cập nhật tất cả các hàng mà tôi đã thay đổi? –

+0

Ý của bạn là gì? Bạn chỉ có thể thay đổi một hàng tại một thời điểm, vì vậy bạn gọi mã mỗi lần thay đổi được thực hiện. – camickr

1

Có thể bạn định sử dụng chỉ mục thay vì hàng trong mệnh đề WHERE?

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

giả sử bạn đã nhiều hàng đã chọn, nếu id của bạn là 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(); 
     } 
    } 

for best performance bạn có thể sử dụng executeBatch như thế này example

2

Đối với hoạt động CRUD trên một tập hợp dữ liệu, nó là tốt đẹp để sử dụng một bảng trung gian. Điều này tránh được số lượng lớn các truy vấn được đặt trên các tập dữ liệu lớn.

Trước khi đưa ra đề nghị của tôi để giải quyết vấn đề, tôi muốn chỉ ra một số nhận xét tôi có vào cấu trúc của cơ sở dữ liệu:

  1. Các total lĩnh vực rõ ràng là một lĩnh vực tính toán. Những thông tin này không tốt để đưa vào cơ sở dữ liệu. Nó được tính theo yêu cầu.
  2. Toàn bộ tập hợp dữ liệu rõ ràng là một phần của tài liệu (hóa đơn). Vì vậy, phải có một trường trong cơ sở dữ liệu xác định duy nhất tài liệu mà dữ liệu có liên quan.

Ngoài ra, tôi muốn nói rằng các quyết định như vậy được thực hiện cho một cơ sở dữ liệu cụ thể. Trong trường hợp này, giải pháp của tôi liên quan đến mysql.

Đây là DDL của bảng mà trên đó các đoạn mã dưới chạy

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 

Và đây là đoạn code mà làm cho các hoạt động CRUD trên một tập hợp dữ liệu sử dụng chìa khóa id tài liệu (invoce_id).

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 là dự án thử nghiệm thể hiện cách mã hoạt động.

Các vấn đề liên quan