我正在使用Java和MySQL开发员工薪资管理系统。当我尝试添加记录时,就会发生此错误。

我的代码在这里:

    try {
         String sql ="insert into Staff_information "
            + "(first_name,surname,Dob,Email,"
            + "Telephone,Address,Department,"
            + "Image,Salary,Gender,Address2,"
            + "Post_code, Designation,Status,job_title,Apartment,Date_hired)          values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?) ";
         pst=conn.prepareStatement(sql);
         pst.setString(1,txt_firstname.getText());
         pst.setString(2,txt_surname.getText());
         pst.setString(3,txt_dob.getText());
         pst.setString(4,txt_email.getText());
         pst.setString(5,txt_tel.getText());
         pst.setString(6,txt_address.getText());
         pst.setString(7,txt_dep.getText());
         pst.setBytes(8,person_image);
         pst.setString(9,txt_salary.getText());
         pst.setString(10,gender);
         pst.setString(11,txt_add2.getText());
         pst.setString(12,txt_pc.getText());
         pst.setString(13,txt_design.getText());
         pst.setString(14,txt_status.getText());
         pst.setString(15,txt_job.getText());
         pst.setString(16,txt_apt.getText());
         pst.setString(17,txt_doj.getText());
         pst.execute();
         JOptionPane.showMessageDialog(null,"Data is saved successfully");
    } catch (Exception e) {
        JOptionPane.showMessageDialog(null,e);
    } finally{
        try {
           rs.close();
           pst.close();
        } catch(Exception e) {
           JOptionPane.showMessageDialog(null,e);
        }
    }
}

最佳答案

您的表还具有一个非空列id(可能是主键),没有默认值。这意味着您必须在查询中分配一个值。

这也可能意味着您打算将其作为标识列(AUTO_INCREMENT),但是忘记将其设置为1。

08-04 23:22