2017-08-25 23 views
5

मुझे सेल मानों को संपादित/बदलकर और अपडेट बटन पर क्लिक करके डेटाबेस से डेटा अपडेट करने में समस्या हो रही है, लेकिन वह उस डेटाबेस को नहीं बदल सकता जिसे मैंने सेल में बदल दिया है। यह अभी भी अंतिम मूल्य में समान है।जावा-अपडेट डेटाबेस डेटाबेस को संपादित करके काम नहीं कर रहा है?

यहाँ मेरी कोड है:

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

एसओ में आपका स्वागत है। प्रदान की गई जानकारी आपकी मदद करने के लिए पर्याप्त नहीं है। 'इंडेक्स' और '(स्ट्रिंग) tableample.getValueAt (पंक्ति, एक्स)' प्रिंटिंग का प्रयास करें। आप आश्चर्यचकित हो सकते हैं। – c0der

+0

मैं पहले ही इंडेक्स डालने का प्रयास करता हूं: स्ट्रिंग एसक्यूएल = "अद्यतन चालान एसईटी विवरण =?, Qty =?, Unitprice =?" + ", कुल =? कहां आईडी =" + सूचकांक; लेकिन केवल अद्यतन ही पहली पंक्ति है और अन्य पहली पंक्ति –

उत्तर

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

आप जहां खंड के लिए एसक्यूएल में एक चर एम्बेड करने के लिए कोशिश कर रहे हैं क्यों?

बस अन्य मानों के लिए आप जैसे पैरामीटर का उपयोग करें। यह एसक्यूएल सरल रखना होगा:

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

धन्यवाद यह वास्तव में एक विशिष्ट पंक्ति को अपडेट करने के लिए काम करता है जिसे मैंने jtable में बदल दिया है, लेकिन अगर मैं उन सभी पंक्तियों को अपडेट करना चाहता हूं जो मैंने बदल दिया है? –

+0

आपका क्या मतलब है? आप एक समय में केवल एक पंक्ति बदल सकते हैं, इसलिए जब भी कोई परिवर्तन किया जाता है तो आप कोड को आवेदक देते हैं। – camickr

1

यह है कि आप कहां खंड में के बजाय सूचकांक पंक्ति का उपयोग करने का इरादा हो सकता है?

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

संभालने आप एक से अधिक पंक्ति का चयन किया है, यदि आपका आईडी पूर्णांक है:

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 है और यह कोड है कि दस्तावेज़ आईडी कुंजी (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 एक परीक्षण परियोजना है कि यह दर्शाता है काम करता है ऊपर कैसे कोड है।

संबंधित मुद्दे