我正在开发一个servlet应用程序,该应用程序在jsp中显示该表,而不是在其他表中插入该表的值(将表的ID插入另一个表),因为我通过调试器进行了检查,从第一个表中读取值没有问题并将其插入到scound表中,但是当我在一段时间后单击插入按钮时,我也遇到了这个错误(java.lang.StackOverflowError),当我检查mysql表时,我得到的值比我将其插入时要多关于我正在读取数据的表的无限循环。(表名:class,id(type:int(primary),null:no,default:none,Extra:AUTO_INCREMENT),name(type:
varchar,null:yes,default:current_timestamp)),我正在插入的表是(表名test,id(type:int(index),null:yes,default:null))
我想再说一遍,除了错误,我还有更多插入,我单击sql表中的jsp(例如多出100倍)
// servlet class
public class add_course extends HttpServlet {
private dbutil dbutil;
@Resource(name="jdbc/web_student_tracker")
private DataSource dataSource;
@Override
public void init() throws ServletException {
//dbutil= new dbutil(dataSource);
super.init();
try {
dbutil=new dbutil(dataSource);
}
catch(Exception exc) {
throw new ServletException(exc);
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// List<student> student;
// try {
// student = dbutil.getcourse();
// request.setAttribute("select",student);
// RequestDispatcher dispatcher = request.getRequestDispatcher("/course.jsp");
// dispatcher.forward(request,response);
// } catch (Exception e) { // TODO Auto-generated catch block
// e.printStackTrace();
// }
try {
String thecommand=request.getParameter("command");
if(thecommand==null) {
thecommand="LIST";
}
switch(thecommand) {
case"LIST":
listcourse(request,response);
break;
case"insert":
insertcourse(request,response);
break;
}
}
catch(Exception exc) {
throw new ServletException(exc);
}
}
private void insertcourse(HttpServletRequest request, HttpServletResponse response) throws Exception {
int courseid = Integer.parseInt(request.getParameter("courseid"));
student thestudent=new student(courseid);
dbutil.insetcourse(thestudent);
insertcourse(request,response);
}
private void listcourse(HttpServletRequest request, HttpServletResponse response) throws Exception {
List<student> student=dbutil.getcourse();
request.setAttribute("select",student);
RequestDispatcher dispatcher = request.getRequestDispatcher("/course.jsp");
dispatcher.forward(request,response);
}
}
// db class
public List <student> getcourse() throws Exception{
List<student> course=new ArrayList<>();
Connection myConn = null;
Statement myStmt = null;
ResultSet myRs = null;
try {
myConn=dataSource.getConnection();
String sql="select id from class";
myStmt=myConn.createStatement();
myRs=myStmt.executeQuery(sql);
while (myRs.next()) {
int id = myRs.getInt("id");
student tempstudent = new student(id);
course.add(tempstudent);
}
return course;
}
finally {
// close JDBC objects
close(myConn, myStmt, myRs);
}
}
public void insetcourse(student thestudent)throws SQLException {
Connection myConn = null;
PreparedStatement myStmt=null;
/////////
try {
myConn = dataSource.getConnection();
String sql="insert into test"+"(id)"+"value(?)";
myStmt=myConn.prepareStatement(sql);
myStmt.setInt(1,thestudent.getId());
myStmt.execute();
}
finally {
close(myConn,myStmt,null);
}
}
}
//jsp page
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<table>
<c:forEach var="tempstudent" items="${select}">
<c:url var="insert" value="add_course">
<c:param name="command" value="insert"/>
<c:param name="courseid" value="${tempstudent.id}"/>
</c:url>
<tr>
<td>${tempstudent.id}</td>
<td>
<a href="${insert}"
onclick="if (!(confirm('Are you sure you want to insert this student?'))) return false">
insert</a>
</td>
</tr>
</c:forEach>
</table>
</body>
</html>
最佳答案
如果要在一个表中插入多个记录,为什么不使用for loop
而不是递归方法。
private void insertcourse(HttpServletRequest request, HttpServletResponse response) throws Exception {
// Assuming you are getting multiple courseid in your request,
Object[] courseid = request.getParameter("courseid");
for(Object obj : courseid)
{
student thestudent=new student(courseid);
dbutil.insetcourse(thestudent);
}
request.setAttribute("message", "Records loaded successfully");
RequestDispatcher dispatcher = request.getRequestDispatcher("/course.jsp");
dispatcher.forward(request,response);
}
插入完成后,您会将结果分配给forntend,以便通知用户,
如果您只想插入一条记录,只需一次调用
insert
。但是查看您的jsp代码,您将为每条记录生成
insert
超链接,因此您一次只能插入一条记录,在这种情况下,您可以遵循以下代码。 int courseid = Integer.parseInt(request.getParameter("courseid"));
student thestudent=new student(courseid);
dbutil.insetcourse(thestudent);
request.setAttribute("message", "Records loaded successfully");
RequestDispatcher dispatcher = request.getRequestDispatcher("/course.jsp");
dispatcher.forward(request,response);
关于java - Servlet Exception(java.lang.StackOverflowError)用于将表中的值插入另一个,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/59996742/