2013-04-06 42 views
0

有很多與此主題相關的問題,但我無法找到解決我的問題的方法。 我有一個「產品」表,我試圖在netbeans中進行更新。 SQL語句在SQL開發工作,並且我已經加倍檢查了我的連接等。在java中運行SQL更新語句

update products 
set pvolume = 2, pprice = 15 
where productid = 3; 

輸出:更新了1行。

但在netbeans中運行它不會執行。如果我錯過了一些小的語法問題,我很抱歉,但我真的需要這種方法的幫助。

public boolean editProduct(int ID, String name, int volume, int quantity, String description, int price) { 
    boolean success = false; 
    Connection con = ConnectionTools.getInstance().getCurrentConnection(); 
    String SQLString1 = "UPDATE products " 
         + "SET pname = ?, " 
         + "pvolume = ?, " 
         + "pquantity = ?, " 
         + "pdescription = ?, " 
         + "pprice = ? " 
         + "WHERE productID = ?"; 
    PreparedStatement statement = null; 
    try { 
     statement = con.prepareStatement(SQLString1); 
     statement.setString(1, name); 
     statement.setInt(2,volume); 
     statement.setInt(3, quantity); 
     statement.setString(4, description); 
     statement.setInt(5, price); 
     statement.setInt(6, ID); 
     statement.executeUpdate(); 
     success = true; 
    }catch (Exception e){ 
     System.out.println("Insertion error!"); 
     System.out.println(e.getMessage()); 
    }finally { 
     try { 
      statement.close(); 
     } catch (SQLException e) { 
      System.out.println("Statement close error!"); 
      System.out.println(e.getMessage()); 
     } 
    } 
    return success; 
} 

通過調試運行,似乎通過嘗試儘可能statement.setInt(6號)運行,但隨後並沒有執行。這裏是輸出:

插入錯誤! ORA-00971:缺少SET關鍵字

任何幫助/建議,將不勝感激!謝謝

回答

0

你必須使用括號:update products set (pvolume = 2, pprice = 15) where productid = 3

+0

括號是完全可選的。請參閱文檔中的update-set子句:http://docs.oracle.com/cd/E11882_01/server.112/e26088/statements_10008.htm#i2126876 – 2013-04-06 11:28:40

+0

只要嘗試執行,仍會使用方括號捕獲異常。除了沒有括號,它在SQL開發工作正常...? – mcabe0131 2013-04-06 11:35:09