While a am trying to insert values to following table via jdbc i am getting error 

Database schema

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

```

Code

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

```

Exception 

```
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)

```