用于将值从 table 插入到另一个值的 Servlet 异常 ( java.lang.StackOverflowError )
Servlet Exception( java.lang.StackOverflowError ) for inserting a value from table to other one
我正在开发一个在 jsp 中显示 table 的 servlet 应用程序,而不是在其他 table 中插入 table 的值(插入来自table 到另一个 table) 当我用调试器检查时,从第一个 table 读取一个值并将其插入到 scound table 没有问题但是当我点击插入按钮时过了一会儿,我也有这个错误(java.lang.WhosebugError),当我检查 mysql table 我明白了,我的价值比我插入它的时间要多,就像无限 loop.about 我正在读取数据的 table 我可以说(table 的名称 :class,id( type:int(primary),null:no,default:none,额外:AUTO_INCREMENT),名称(类型:
varchar,null:yes,default:current_timestamp)) 和 table 我插入的值是(table name test,id(type:int(index),null:是的,默认值:null))
我想再说一遍,除了错误之外,我在 sql jsp 中单击了更多插入 table(大约 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>
如果要在 table 中插入多条记录,为什么不使用 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
hyper link,因此您一次只能插入一条记录,在这种情况下,您可以按照下面的代码。
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);
我正在开发一个在 jsp 中显示 table 的 servlet 应用程序,而不是在其他 table 中插入 table 的值(插入来自table 到另一个 table) 当我用调试器检查时,从第一个 table 读取一个值并将其插入到 scound table 没有问题但是当我点击插入按钮时过了一会儿,我也有这个错误(java.lang.WhosebugError),当我检查 mysql table 我明白了,我的价值比我插入它的时间要多,就像无限 loop.about 我正在读取数据的 table 我可以说(table 的名称 :class,id( type:int(primary),null:no,default:none,额外:AUTO_INCREMENT),名称(类型: varchar,null:yes,default:current_timestamp)) 和 table 我插入的值是(table name test,id(type:int(index),null:是的,默认值:null))
我想再说一遍,除了错误之外,我在 sql jsp 中单击了更多插入 table(大约 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>
如果要在 table 中插入多条记录,为什么不使用 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
hyper link,因此您一次只能插入一条记录,在这种情况下,您可以按照下面的代码。
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);