2013-03-31 70 views
0

基本上我試圖用getSelectRow的值更新數據庫表。正如你所看到的,查詢找到了正確的數據,但實際上試圖將其添加到數據庫時遇到了很多問題。
錯誤出現在SQL語法中,但我不知道我要出錯的地方。請幫忙。INSERT語句中的未知SQL問題

這是它執行的查詢,但我不知道它爲什麼不更新表。

INSERT INTO customerdetails 
     FName  = 'Tim' 
    AND SName  = 'Cooley' 
    AND Address  = '52  Buckminster Drive Dorridge Solihull West Mids' 
    AND Postcode  = 'B93 8PG' 

Java代碼:

private void sendBtnMouseClicked(java.awt.event.MouseEvent evt) {          
    // TODO add your handling code here: 

    int insertRow = newOrderTbl.getSelectedRow(); 
    int col2 = 0; 

    String sql3 = "INSERT INTO customerdetails VALUES " 
      + "FName   = '" + newOrderTbl.getValueAt(insertRow, col2)  +"'" 
      + "AND SName  = '" + newOrderTbl.getValueAt(insertRow, col2+1) +"'" 
      + "AND Address  = '" + newOrderTbl.getValueAt(insertRow, col2+2) +"'" 
      + "AND Postcode  = '" + newOrderTbl.getValueAt(insertRow, col2+3) +"'"; 
    System.out.println(sql3); 
    try{ 

     pst = conn.prepareStatement(sql3); 
     pst.executeUpdate(sql3); 
     JOptionPane.showMessageDialog(null, "Deleted"); 


     CustomerTable(); 

    } 
    catch (Exception e){ 
     JOptionPane.showMessageDialog(null, e); 
    } 


} 
+2

您有一個SQL注入漏洞。 – SLaks

+0

我知道它的確如此,但這並不重要,因爲這是一個永遠不會在任何地方發佈的個人項目。 錯誤是我需要幫助的。 –

+0

pst.executeUpdate(...)'的返回值是什麼?根據Javadoc的說法,它返回「(1)SQL數據操作語言(DML)語句的行數或(2)0語句不返回任何內容的SQL語句」。 – mthmulders

回答

3

首先,您的SQL語法錯誤(至少這是你的數據庫引擎非標準的SQL語法)。其次,您的代碼容易受到SQL注入攻擊。

爲了解決這兩個問題,你應該使用PreparedStatement(你這樣做的方式是錯誤的)。從代碼中一個基本的例子:

String sql = "INSERT INTO customerdetails (FName, SName, Address, Postcode) VALUES (?, ?, ?,?)"; 
PreparedStatement pst = conn.prepareStatemtnt(sql); 
pst.setString(1, newOrderTbl.getValueAt(insertRow, col2)); 
pst.setString(2, newOrderTbl.getValueAt(insertRow, col2+1)); 
pst.setString(3, newOrderTbl.getValueAt(insertRow, col2+2)); 
pst.setString(4, newOrderTbl.getValueAt(insertRow, col2+3)); 
pst.executeUpdate(); 
//rest of code... 

假設你的SQL語法將工作,那麼你應該通過值作爲參數,類似於前面的例子:

String sql3 = "INSERT INTO customerdetails VALUES " 
     + "FName   = ?" 
     + "AND SName  = ?" 
     + "AND Address  = ?" 
     + "AND Postcode  = ?" 
pst = conn.prepareStatement(sql3); 
pst.setString(1, newOrderTbl.getValueAt(insertRow, col2)); 
pst.setString(2, newOrderTbl.getValueAt(insertRow, col2+1)); 
pst.setString(3, newOrderTbl.getValueAt(insertRow, col2+2)); 
pst.setString(4, newOrderTbl.getValueAt(insertRow, col2+3)); 
pst.executeUpdate(); 
//rest of code... 
0

for update語句這將是 -

String sql3 = "INSERT INTO customerdetails(FName,SName,Address,Postcode) VALUES " 
      + " '" + newOrderTbl.getValueAt(insertRow, col2)  +"'," 
      + " '" + newOrderTbl.getValueAt(insertRow, col2+1) +"'," 
      + " '" + newOrderTbl.getValueAt(insertRow, col2+2) +"'," 
      + " '" + newOrderTbl.getValueAt(insertRow, col2+3) + "')"; 

此外,你應該使用PreparedStatement。

感謝

0

請其更改爲

String sql3 = "INSERT INTO customerdetails(FName,SName,Address,Postcode) VALUES (" 
      + "'" + newOrderTbl.getValueAt(insertRow, col2)  +"'" 
      + "'" + newOrderTbl.getValueAt(insertRow, col2+1) +"'" 
      + "'" + newOrderTbl.getValueAt(insertRow, col2+2) +"'" 
      + "'" + newOrderTbl.getValueAt(insertRow, col2+3) +"')"; 

在你的代碼生成的INSERT語句似乎無效。請參閱SQL Insert Statement以獲取更多信息

此外,更好的方法是創建專用的Serverside DAO類來處理數據庫操作。

+0

你達人! Ty幫忙:) –