我正在構建一個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
閱讀郵件,幾個數據庫的問題(列,表),我們不能說withou功能的Java代碼和數據庫結構 –
它看起來像你試圖插入一個測試,老師不在數據庫中存在。 –
我編輯了問題並在其中添加了查詢頁面。 – rango