2017-08-11 125 views
0

我正在構建一個jsp項目,並且爲它創建了一個數據庫,其中包含多個表和外鍵。 和我做了一個簡單的頁面插入一些實體在我的表中,但我得到了這個錯誤。自從一週以來我一直在使用jsp,之前我在數據庫表中插入了實體,但從未得到這個錯誤,我已經搜索了很多關於這個但仍然在這個錯誤中的webbed。請幫我解決這個問題,並告訴我我錯在哪裏。mysql約束錯誤

<%@page contentType="text/html" language="java" pageEncoding="UTF-8"%> 
<%@page import="java.sql.*" %> 
<!DOCTYPE html> 
<html> 
    <head> 
     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
     <title>Add Test</title> 
    </head> 
    <body> 

     <%@include file="header.jsp" %> 

      <div class="jumbotron"> 
    <div class="container">   
     <form action="saveTest.jsp"> 

           <div class="row"> 
        <div class="col-lg-offset-4 col-lg-2"> 
              <h3>Test Title</h3> 
        </div> 
        <div class="col-lg-4"> 
              <h3><input type="text" class="form-control" name="test" required></h3> 
        </div> 

       </div> 

           <div class="row"> 
        <div class="col-lg-offset-4 col-lg-2"> 
              <h3>Description</h3> 
        </div> 
        <div class="col-lg-4"> 
              <h3><textarea type="text" class="form-control" name="des"></textarea></h3> 
        </div> 

       </div> 



           <div class="row"> 
        <div class="col-lg-offset-6 col-lg-2"> 
              <h3><input type="submit" value="Add Test" class="form-control" ></h3> 
        </div> 


       </div> 
      </form> 
        </div> 
      </div> 
     <%@include file="footer.jsp" %> 
    </body> 
</html> 
下面

是頁面,查詢是:

<%@page contentType="text/html" language="java" pageEncoding="UTF-8"%> 
<%@page import="java.sql.*" %> 
<!DOCTYPE html> 
<html> 
    <head> 
     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
     <title>Save Test</title> 
    </head> 
    <body> 
     <% 
     String test=request.getParameter("test"); 
     String des=request.getParameter("des"); 

     Class.forName("com.mysql.jdbc.Driver"); 
     Connection con=DriverManager.getConnection("jdbc:mysql://localhost/exam","root","password"); 
     String q="insert into test(testname,description) values(?,?);"; 
     PreparedStatement p=con.prepareStatement(q); 
     p.setString(1, test); 
     p.setString(2, des); 
     p.executeUpdate(); 
     con.close(); 
     response.sendRedirect("addQuestion.jsp"); 
     %> 
    </body> 
</html> 

,我得到的錯誤是:

HTTP Status 500 - javax.servlet.ServletException: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Cannot add or update a child row: a foreign key constraint fails (`exam`.`test`, CONSTRAINT `testteacherid` FOREIGN KEY (`teacherid`) REFERENCES `teacher` (`teacherid`) ON DELETE NO ACTION ON UPDATE NO ACTION) 

type Exception report 

message javax.servlet.ServletException: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Cannot add or update a child row: a foreign key constraint fails (`exam`.`test`, CONSTRAINT `testteacherid` FOREIGN KEY (`teacherid`) REFERENCES `teacher` (`teacherid`) ON DELETE NO ACTION ON UPDATE NO ACTION) 

description The server encountered an internal error that prevented it from fulfilling this request. 

exception 

org.apache.jasper.JasperException: javax.servlet.ServletException: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Cannot add or update a child row: a foreign key constraint fails (`exam`.`test`, CONSTRAINT `testteacherid` FOREIGN KEY (`teacherid`) REFERENCES `teacher` (`teacherid`) ON DELETE NO ACTION ON UPDATE NO ACTION) 
    org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:555) 
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:461) 
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:396) 
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:340) 
    javax.servlet.http.HttpServlet.service(HttpServlet.java:729) 
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) 
    org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393) 
root cause 

javax.servlet.ServletException: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Cannot add or update a child row: a foreign key constraint fails (`exam`.`test`, CONSTRAINT `testteacherid` FOREIGN KEY (`teacherid`) REFERENCES `teacher` (`teacherid`) ON DELETE NO ACTION ON UPDATE NO ACTION) 
    org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:909) 
    org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:838) 
    org.apache.jsp.saveTest_jsp._jspService(saveTest_jsp.java:151) 
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70) 
    javax.servlet.http.HttpServlet.service(HttpServlet.java:729) 
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:438) 
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:396) 
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:340) 
    javax.servlet.http.HttpServlet.service(HttpServlet.java:729) 
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) 
    org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393) 
root cause 

com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Cannot add or update a child row: a foreign key constraint fails (`exam`.`test`, CONSTRAINT `testteacherid` FOREIGN KEY (`teacherid`) REFERENCES `teacher` (`teacherid`) ON DELETE NO ACTION ON UPDATE NO ACTION) 
    sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) 
    sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) 
    sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) 
    java.lang.reflect.Constructor.newInstance(Constructor.java:423) 
    com.mysql.jdbc.Util.handleNewInstance(Util.java:425) 
    com.mysql.jdbc.Util.getInstance(Util.java:408) 
    com.mysql.jdbc.SQLError.createSQLException(SQLError.java:935) 
    com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3973) 
    com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.ja 
+0

閱讀郵件,幾個數據庫的問題(列,表),我們不能說withou功能的Java代碼和數據庫結構 –

+0

它看起來像你試圖插入一個測試,老師不在數據庫中存在。 –

+0

我編輯了問題並在其中添加了查詢頁面。 – rango

回答

0

的錯誤是非常直截了當。其給出約束違規錯誤。這通常只發生在子表正在具有外鍵關係的列中插入值並且引用外表時沒有此值。

列「testteacherid」應該具有來自表「teacher」的「teacherid」列中的一個值。

「不能添加或更新子行,外鍵約束失敗(examtest,約束testteacherid外鍵(teacherid)參考文獻teacherteacherid)ON DELETE NO ACTION ON UPDATE NO ACTION)」。

它的問題與您的代碼沒有關係,它與您嘗試插入的數據值有關。 父表必須具有子表引用的值。

如果你會給你想同時插入表值和新的價值觀,我們就可以在插入值指出錯誤。

+0

非常感謝你的幫助。 – rango