行插入时出现外键冲突错误

时间:2015-02-19 10:00:03

标签: java jdbc

当我试图通过jdbc向下表插入值时,我收到错误

数据库架构

CREATE TABLE student_contact_details_t(
    student_contact_details_id MEDIUMINT NOT NULL AUTO_INCREMENT,
    student_id MEDIUMINT NOT NULL ,
    contact_relation_type_id MEDIUMINT NOT NULL,
    name varchar(255),
    addresss varchar(255),
    phone_no varchar(255),
    created_by varchar(255),
    created_date DATE,
    modified_by varchar(255),
    modified_date DATE,
    PRIMARY KEY(student_contact_details_id),
    FOREIGN KEY(student_id) REFERENCES student_details_t(student_id),
    FOREIGN KEY(contact_relation_type_id) REFERENCES contact_relation_type_t(contact_relation_type_id)
);

代码

public void saveStudentContactDetails(
        StudentContactDetailsTO studentContactDetails)
        throws ClassNotFoundException, SQLException, IOException {
    try {
        int autoId = 0;
        String Insertquery = "insert into student_contact_details_t(student_id,contact_relation_type_id,name,addresss,phone_no,created_by,created_date,modified_by,modified_date)values(?,?,?,?,?,?,?,?,?)";
        PreparedStatement prepareStatement = JDBCConnectionUtil
                .getConnection().prepareStatement(Insertquery,
                        Statement.RETURN_GENERATED_KEYS);
        ResultSet studentId = prepareStatement.getGeneratedKeys();

        prepareStatement.setInt(1, studentContactDetails.getStudentId());

        // prepareStatement.setInt(2,studentId.getInt(1));
        prepareStatement.setInt(2, autoId);
        prepareStatement.setString(3, studentContactDetails.getName());
        prepareStatement.setString(4, studentContactDetails.getAddress());
        prepareStatement.setString(5, studentContactDetails.getPhoneNo());
        prepareStatement.setString(6, "sysadmin");
        prepareStatement.setString(7, DateUtil.getDate().format(date));
        prepareStatement.setString(8, "sysadmin");
        prepareStatement.setString(9, DateUtil.getDate().format(date));
        prepareStatement.executeUpdate();
        while (studentId.next()) {
            autoId = studentId.getInt(1);
            System.out.println(autoId);
        }
    } catch (ClassNotFoundException | SQLException | IOException e) {
        throw e;
    }
}

例外

com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Cannot add or update a child row: a foreign key constraint fails (`studentregistration`.`student_contact_details_t`, CONSTRAINT `student_contact_details_t_ibfk_1` FOREIGN KEY (`student_id`) REFERENCES `student_details_t` (`student_id`))
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:408)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:377)
    at com.mysql.jdbc.Util.getInstance(Util.java:360)

2 个答案:

答案 0 :(得分:1)

错误表示您在表student_id中插入了student_contact_details_t student_details_t表中不存在的student_contact_details_t表示在student_details_t表创建期间设置的外键约束

所以你必须先在表student_contact_details_t中插入,然后在student_id中插入{{1}}

答案 1 :(得分:0)

您必须验证学生表student_details_t中是否存在学生ID。

如果学生在student_details_t中不存在,则无法在相关表格中插入student_contact_details_t。

在这种情况下,您应该告诉用户“学生XXX不存在”。