我正在开发一个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
而不是递归方法。