我正在尝试将数据插入数据库,但是当我点击按钮插入时出现错误。
这是错误
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);
}
}
答案 0 :(得分: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();
}
答案 1 :(得分:-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和它(但正确;-))它应该工作
答案 2 :(得分:-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();
}
感谢大家的帮助