简体   繁体   English

java.sql.SQLException:field id 没有默认值

[英]java.sql.SQLException:field id does not have a default value

I'm developing an Employee Payroll Management system using Java and MySQL.我正在使用 Java 和 MySQL 开发员工薪资管理系统。 When I'm trying to add a record then this error is occuring.当我尝试添加记录时,就会发生此错误。

My code is here:我的代码在这里:

    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);
        } 
    }
}                                        

Your table also has a not-null column id (probably the primary key), without a default value. 您的表还具有一个非空的列id (可能是主键),没有默认值。 That means that you must assign a value in your query. 这意味着您必须在查询中分配一个值。

It could also mean that you meant for it to be an identity column ( AUTO_INCREMENT ), but forgot to set it as one. 这也可能意味着您打算将其作为标识列( AUTO_INCREMENT ),但是忘记将其设置为1。

Only reason for this error could be that you have a column id in your table Staff_information that can't be NULL and you haven't passed a value for it in your query. 发生此错误的唯一原因可能是您在表Staff_information中具有不能为NULL的列id ,并且您没有在查询中为其传递值。

Either pass a value for column id in your query or if it is primary key , set it to auto increment 在您的查询中传递列id的值,或者如果它是primary key ,请将其设置为auto increment

I think you have a column with name "id" which might configured as not null or unique or primarykey . 我认为您有一列名为“ id”的列,该列可能配置not nulluniqueprimarykey

As you are not supplying value for that column your JDBC driver is throwing error. 由于您没有为该列提供值,因此JDBC driver将引发错误。

To resolve this you can explicitly supply the value adding the column in your SQL query or else you can configure that column to be auto increment if the column type is number. 要解决此问题,您可以在SQL查询中显式提供添加列的值,或者,如果列类型为number,则可以将该列配置为auto increment

Other than this as the query you are trying to execute is non-select query it won't return a ResulSet . 除此之外,由于您要执行的查询是非选择查询,因此不会返回ResulSet So, the statement rs.close() is not require. 因此,不需要rs.close()语句。

Also I suggest you to use The try-with-resources Statement . 我也建议您使用try-with-resources语句 Using this you can avoid below statements in your code. 使用此方法,可以避免代码中出现以下语句。

try{
   rs.close();
        pst.close(); 
} catch(Exception e) { 
   JOptionPane.showMessageDialog(null,e); } }
}

Probably your table Staff_information has an ID column, which is mandatory (not nullable ) and the value is not supplied in the insert query. 您的表Staff_information可能有一个ID列,该列是必需的(不能为null),并且在插入查询中未提供该值。

Please check your table definition. 请检查您的表定义。 If the ID column is primary key, then you need to pass the value in the query or define the column to be auto increment 如果ID列是主键,则需要在查询中传递值或将列定义为自动递增

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Java 实体 java.sql.SQLException:字段“id”没有默认值 - Java Entity java.sql.SQLException: Field 'id' doesn't have a default value java.sql.SQLException:字段没有默认值 - java.sql.SQLException: Field doesn't have a default value java.sql.SQLException: 字段 'supplier_id' 没有默认值 - java.sql.SQLException: Field 'supplier_id' doesn't have a default value Spring Boot java.sql.SQLException:字段“id”没有默认值 - Spring Boot java.sql.SQLException: Field 'id' doesn't have a default value java.sql.SQLException:字段“participant_one_fk_id”没有默认值 - java.sql.SQLException: Field 'participant_one_fk_id' doesn't have a default value java.sql.SQLException:字段“id”没有默认值 - java.sql.SQLException: Field 'id' doesn't have a default value java.sql.SQLException:字段“register_id”没有默认值 - java.sql.SQLException: Field 'register_id' doesn't have a default value java.sql.SQLException:字段“ client_id”没有默认值 - java.sql.SQLException: Field 'client_id' doesn't have a default value java.sql.SQLException:“字段‘voc_id’没有默认值”。 使用 Spring JPA - java.sql.SQLException: 'Field 'voc_id' doesn't have a default value'. using Spring JPA 具有一对多关系映射的“ java.sql.SQLException:字段'id_parent'没有默认值” - “java.sql.SQLException: Field 'id_parent' doesn't have a default value” with one-to-many relationship mapping
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM