Referential integrity constraint violation. H2 database -
im using h2 database. im have tables student(n_zk,fio,propiska....etc) , ekzamen(n_zk, pred, ocenka). ekzamen contains foreign key n_zk student. pieces of code:
connecting database:
public studentdao() throws exception { string url = "jdbc:h2:file:e:/kursach/db/db"; string user = "weaver"; string password = "12345"; // connect database myconn = drivermanager.getconnection(url,user,password); system.out.println("db connection successful to: " + url); }
method addekzamen:
public void addekzamen(ekzamen theekzamen) throws exception { preparedstatement mystmt = null; try { mystmt = myconn.preparestatement( "insert ekzamen " + " (n_zk, pred, ocenka)" + " values(?, ?, ?)"); //установить значения mystmt.setint(1, theekzamen.getnzk()); mystmt.setstring(2, theekzamen.getpred()); mystmt.setint(3, theekzamen.getocenka()); //применить sql mystmt.executeupdate(); } { close(mystmt); } }
main:
public static void main(string[] args) throws exception { studentdao dao = new studentdao(); ekzamen e = new ekzamen(17,"economic",5); dao.addekzamen(e); }
im getting error:
exception in thread "main" org.h2.jdbc.jdbcsqlexception:
referential integrity constraint violation: "student: public.ekzamen foreign key(n_zk) references public.student(n_zk) (0)"; sql statement: insert ekzamen (n_zk, pred, ocenka) values(?, ?, ?) [23506-176] @ org.h2.message.dbexception.getjdbcsqlexception(dbexception.java:344) @ org.h2.message.dbexception.get(dbexception.java:178) @ org.h2.message.dbexception.get(dbexception.java:154) @ org.h2.constraint.constraintreferential.checkrowowntable(constraintreferential.java:373) @ org.h2.constraint.constraintreferential.checkrow(constraintreferential.java:315) @ org.h2.table.table.fireconstraints(table.java:909) @ org.h2.table.table.fireafterrow(table.java:927) @ org.h2.command.dml.insert.insertrows(insert.java:162) @ org.h2.command.dml.insert.update(insert.java:115) @ org.h2.command.commandcontainer.update(commandcontainer.java:79) @ org.h2.command.command.executeupdate(command.java:254) @ org.h2.jdbc.jdbcpreparedstatement.executeupdateinternal(jdbcpreparedstatement.java:158) @ org.h2.jdbc.jdbcpreparedstatement.executeupdate(jdbcpreparedstatement.java:144) @ studenttest.studentdao.addekzamen(studentdao.java:68) @ studenttest.studentdao.main(studentdao.java:394)
Comments
Post a Comment