Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java MySQLIntegrityConstraintViolationException-运行DAO项目_Java_Mysql_Sql_Intellij Idea_Runtime Error - Fatal编程技术网

Java MySQLIntegrityConstraintViolationException-运行DAO项目

Java MySQLIntegrityConstraintViolationException-运行DAO项目,java,mysql,sql,intellij-idea,runtime-error,Java,Mysql,Sql,Intellij Idea,Runtime Error,我为easydao程序创建了数据库和两个表。DB是Mysql,在创建时我使用了terminal。以下是两个表的创建代码: CREATE TABLE students ( id INT NOT NULL AUTO_INCREMENT, name VARCHAR(40), course INT, email VARCHAR(40), department_id INT NOT NULL, PRIMARY KEY(id), FOREIGN KEY

我为easydao程序创建了数据库和两个表。DB是Mysql,在创建时我使用了terminal。以下是两个表的创建代码:

CREATE TABLE students (
    id INT NOT NULL AUTO_INCREMENT,
    name VARCHAR(40),
    course INT,
    email VARCHAR(40),
    department_id INT NOT NULL,
    PRIMARY KEY(id),
    FOREIGN KEY(department_id) REFERENCES departments(department_id)
);

CREATE TABLE departments (
    department_id INT NOT NULL AUTO_INCREMENT,
    name VARCHAR(20),
    PRIMARY KEY(department_id)
);
我使用IDE Intellij。它支持智能数据库

你可以看到,它看起来怎么样

com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Cannot add or update a child row: a foreign key constraint fails (`University`.`students`, CONSTRAINT `students_ibfk_1` FOREIGN KEY (`department_id`) REFERENCES `departments` (`department_id`))
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
    at com.mysql.jdbc.Util.getInstance(Util.java:386)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1041)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4237)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4169)
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2617)
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2778)
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2825)
    at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2156)
    at com.mysql.jdbc.PreparedStatement.execute(PreparedStatement.java:1379)
    at lab.epam.controller.dao.StudentDAO.save(StudentDAO.java:82)
    at lab.epam.DAOMain.main(DAOMain.java:20)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
public class DAOMain {

    public static void main(String[] args) {
        StudentDAO studentDAO = new StudentDAO();
        Student student = new Student();
        student.setId(9);
        student.setName("Test");
        student.setCourse(5);
        student.setEmail("student.name@dot.com");
        student.setDepartmentId(16);
        studentDAO.save(student);
        System.out.println(studentDAO.getById(9));
        studentDAO.deleteById(9);
        System.out.println(new DepartmentDAO().getById(12));
    }

但是当我运行我的代码时,我看到了下一批异常:

com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Cannot add or update a child row: a foreign key constraint fails (`University`.`students`, CONSTRAINT `students_ibfk_1` FOREIGN KEY (`department_id`) REFERENCES `departments` (`department_id`))
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
    at com.mysql.jdbc.Util.getInstance(Util.java:386)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1041)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4237)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4169)
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2617)
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2778)
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2825)
    at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2156)
    at com.mysql.jdbc.PreparedStatement.execute(PreparedStatement.java:1379)
    at lab.epam.controller.dao.StudentDAO.save(StudentDAO.java:82)
    at lab.epam.DAOMain.main(DAOMain.java:20)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
public class DAOMain {

    public static void main(String[] args) {
        StudentDAO studentDAO = new StudentDAO();
        Student student = new Student();
        student.setId(9);
        student.setName("Test");
        student.setCourse(5);
        student.setEmail("student.name@dot.com");
        student.setDepartmentId(16);
        studentDAO.save(student);
        System.out.println(studentDAO.getById(9));
        studentDAO.deleteById(9);
        System.out.println(new DepartmentDAO().getById(12));
    }
这是我的main()

com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Cannot add or update a child row: a foreign key constraint fails (`University`.`students`, CONSTRAINT `students_ibfk_1` FOREIGN KEY (`department_id`) REFERENCES `departments` (`department_id`))
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
    at com.mysql.jdbc.Util.getInstance(Util.java:386)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1041)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4237)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4169)
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2617)
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2778)
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2825)
    at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2156)
    at com.mysql.jdbc.PreparedStatement.execute(PreparedStatement.java:1379)
    at lab.epam.controller.dao.StudentDAO.save(StudentDAO.java:82)
    at lab.epam.DAOMain.main(DAOMain.java:20)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
public class DAOMain {

    public static void main(String[] args) {
        StudentDAO studentDAO = new StudentDAO();
        Student student = new Student();
        student.setId(9);
        student.setName("Test");
        student.setCourse(5);
        student.setEmail("student.name@dot.com");
        student.setDepartmentId(16);
        studentDAO.save(student);
        System.out.println(studentDAO.getById(9));
        studentDAO.deleteById(9);
        System.out.println(new DepartmentDAO().getById(12));
    }
为什么会发生这种异常

我不知道我错过了什么

更新:

这是

我修正了一些观点,但它并不是我想要的

以下是固定输出:

学生{id=9,姓名='super Student',课程=5,电子邮件='super。student@dot.com“,department=null,departmentId=14}


有什么建议吗?

由于外键引用,问题出现了。当您试图使用department_id值插入Student表时,MySQL正在检查department表中的行是否具有相同的id。如果没有,您将获得MySQLIntegrityConstraintViolationException

请查看部门表,您将找不到id为16的行

由于您使用的是DAO,您应该先保存系,然后保存学生来解决此问题

查看您的POJO,部门中的POJO列名和表列名不同

另一件事是,当您调用Student save时,部门对象是如何保存的


我是DAO新手,所以这里可能遗漏了一些内容。

什么是ConstraintViolatedException意味着您将一条记录插入到一个表中,该表引用了第二个不存在的值

在表students中,您将department_id作为外键,但尝试在departments表包含students表中引用的值之前将一条记录插入students,写入:

学生编号:setDepartmentId(16)


在将包含所需系id的记录插入学生之前,先在新系的系中插入记录。

请共享您的POJO文件。