gpt4 book ai didi

java - 将值从表插入到另一个表时发生 Servlet 异常( java.lang.StackOverflowError )

转载 作者:行者123 更新时间:2023-12-01 18:38:18 27 4
gpt4 key购买 nike

我正在开发一个servlet应用程序,它在jsp中显示表,然后在其他表中插入该表的值(将id从表插入到另一个表),因为我用调试器检查过,读取值没有问题从第一个表并将其插入到 scound 表中,但是当我单击插入按钮后,我遇到了此错误(java.lang.StackOverflowError),当我检查 mysql 表时,我看到,我的值比插入的时间多它就像一个无限循环。关于我正在读取数据的表,我可以说(表的名称:class,id(类型:int(primary),null:no,default:none,Extra:AUTO_INCRMENT),name(类型: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循环而不是递归方法。

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 异常( java.lang.StackOverflowError ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59996742/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com