我試圖插入數據到數據庫中,但當單擊按鈕插入時出現錯誤。插入到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);
}
}
1.所有**停止連接字符串來構建查詢。使用預先準備好的語句!** 2.錯誤信息對於這個問題非常明確......我不知道有什麼令人困惑的。你明確地說你想要「INSERT」13列,並且只提供12. – Siyual
你能打印出你想要執行的SQL語句並與我們分享嗎? – Mureinik
我的朋友鮑比桌子喜歡這樣的代碼。 http://bobby-tables.com/ –