2017-06-02 166 views
-1

我試圖插入數據到數據庫中,但當單擊按鈕插入時出現錯誤。插入到sql server數據庫錯誤

這是錯誤

com.microsoft.sqlserver.jdbc.SQLServerException: There are more columns in the INSERT 
statement than values specified in the VALUES clause. The number of values in the VALUES 
clause must match the number of columns specified in the INSERT statement. 

我想您的幫助,如果你能找出問題。

這是我的插入代碼

private void insertActionPerformed(java.awt.event.ActionEvent evt) {          
    // TODO add your handling code here: 
    dbconnection db = new dbconnection(); 
    try { 
     db.connect(); 
     db.stm=db.con.createStatement(); 
     java.sql.Date date1 = new java.sql.Date(jDateChooser1.getDate().getTime()); 
      int result=db.stm.executeUpdate("insert into Blood_Test_Result" +"(DID,D_Name,Weight,HBsAG,HIV,VDRL,HCV,Malaria,Blood_Type,Blood_Status,LTID,LT_Name,Date)" 
        +"values('"+jComboBox2.getSelectedItem().toString()+"'," 
        + "'"+jTextField1.getText()+"','"+jTextField3.getText()+"','"+jComboBox4.getSelectedItem().toString()+"'," 
        + "'"+jComboBox5.getSelectedItem().toString()+"','"+jComboBox6.getSelectedItem().toString()+"'," 
        + "'"+jComboBox7.getSelectedItem().toString()+"','"+jComboBox8.getSelectedItem().toString()+"'" 
        + "'"+jComboBox9.getSelectedItem().toString()+"','"+jComboBox10.getSelectedItem().toString()+"'," 
        + "'"+jComboBox3.getSelectedItem().toString()+"','"+jTextField2.getText()+"','"+date1+"')"); 
     if(result>0) 
     { 
      JOptionPane.showMessageDialog(this, "Data has been saved succesfully");    
     } 
     else 
     { 
      JOptionPane.showMessageDialog(this, "no data has been saved"); 
     } 

    } catch (SQLException ex) { 
     Logger.getLogger(BloodTest.class.getName()).log(Level.SEVERE, null, ex); 
    } 

}      
+6

1.所有**停止連接字符串來構建查詢。使用預先準備好的語句!** 2.錯誤信息對於這個問題非常明確......我不知道有什麼令人困惑的。你明確地說你想要「INSERT」13列,並且只提供12. – Siyual

+0

你能打印出你想要執行的SQL語句並與我們分享嗎? – Mureinik

+1

我的朋友鮑比桌子喜歡這樣的代碼。 http://bobby-tables.com/ –

回答

4

的錯誤是明顯的,你在使用的13列。

(BTRID,DID,D_Name,Weight,HBsAG,HIV,VDRL,HCV,Malaria,Blood_Type,Blood_Status,LTID,LT_Name) 

但你設置值12值:

values(....) 

所以一步檢查查詢步驟,並確保您使用的是正確的列。


我的回答是這個重要的組成部分,不設置你的屬性就是這樣,而是使用PreparedStatement的,以避免語法錯誤和SQL注入:

String query = "insert into Blood_Test_Result" + "(BTRID, DID ,D_Name, " 
     + "Weight, HBsAG, HIV, VDRL, HCV, Malaria, Blood_Type, Blood_Status, LTID,LT_Name)" 
     + "values(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"; 

try (PreparedStatement insert = connection.prepareStatement(query)) { 

    insert.setString(1, jComboBox2.getSelectedItem().toString()); 
    insert.setString(2, jTextField1.getText()); 
    ... 

    insert.executeUpdate(); 
} 
+0

SORRY,我發佈了錯誤的數據這是實際數據 –

+1

你是什麼意思@YusufMohamed? –

+0

請你再看看代碼,因爲我更新了它。 –

-1

的錯誤是很清楚的:

你的評論中有更多的列比值!

 (BTRID,DID,D_Name,Weight,HBsAG,HIV,VDRL,HCV,Malaria,Blood_Type,Blood_Status,LTID,LT_Name) 

這是13列,你必須

jComboBox2.getSelectedItem().toString()+"'," 
       + "'"+jTextField1.getText()+"','"+jTextField3.getText()+"','"+jComboBox4.getSelectedItem().toString()+"'," 
       + "'"+jComboBox5.getSelectedItem().toString()+"','"+jComboBox6.getSelectedItem().toString()+"'," 
       + "'"+jComboBox7.getSelectedItem().toString()+"','"+jComboBox8.getSelectedItem().toString()+"'" 
       + "'"+jComboBox9.getSelectedItem().toString()+"','"+jComboBox10.getSelectedItem().toString()+"'," 
       + "'"+jComboBox3.getSelectedItem().toString()+"','"+jTextField2.getText()+ 

只有12個值,以便去除colmun和它(但正確的;-)),它應該工作

+1

這只是持續的sql注入。這需要參數化才能被認爲是可行的答案。 –

-1

我解決了這個錯誤,因爲我錯過了兩列之間的逗號。

private void insertActionPerformed(java.awt.event.ActionEvent evt) {          
    // TODO add your handling code here: 
    dbconnection db = new dbconnection(); 
    try { 
     db.connect(); 
     db.stm=db.con.createStatement(); 
     java.sql.Date date1 = new java.sql.Date(jDateChooser1.getDate().getTime()); 
      int result=db.stm.executeUpdate("insert into Blood_Test_Result" +"(DID,D_Name,Weight,HBsAG,HIV,VDRL,HCV,Malaria,Blood_Type,Blood_Status,LTID,LT_Name,Date)" 
        +"values('"+jComboBox2.getSelectedItem().toString()+"'," 
        + "'"+jTextField1.getText()+"','"+jTextField3.getText()+"','"+jComboBox4.getSelectedItem().toString()+"'," 
        + "'"+jComboBox5.getSelectedItem().toString()+"','"+jComboBox6.getSelectedItem().toString()+"'," 
        + "'"+jComboBox7.getSelectedItem().toString()+"','"+jComboBox8.getSelectedItem().toString()+"'," 
        + "'"+jComboBox9.getSelectedItem().toString()+"','"+jComboBox10.getSelectedItem().toString()+"'," 
        + "'"+jComboBox3.getSelectedItem().toString()+"','"+jTextField2.getText()+"','"+ date1 +"')"); 
     JOptionPane.showMessageDialog(this, "insert successful"); 

    } catch (SQLException ex) { 
     JOptionPane.showMessageDialog(this, ex.getMessage()); 
    } 
    fill(); 
    clear(); 

}        

感謝您的幫助

+1

這不是一個好的解決方案,因爲你的代碼容易受到SQL注入攻擊,這是一個巨大的安全漏洞。按照[YCF_L的建議](https://stackoverflow.com/a/44330401/466862)使用準備好的語句。 –