-1
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.ws.Response;

import com.sun.corba.se.spi.activation.Repository;

/**
 * Servlet implementation class DeleteControl
 */
public class DeleteControl extends HttpServlet {
    private static final long serialVersionUID = 1L;

    ResultSet rs = null;
    Statement st = null;
    /**
     * @see HttpServlet#HttpServlet()
     */
    public DeleteControl() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        String str = request.getParameter("hiddenValue");
        try{
            Class.forName("com.mysql.jdbc.Driver");

            Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/Student","root","root");
            //here sonoo is the database name, root is the username and root is the password

            PreparedStatement ps = con.prepareStatement("delete from login where studentId= ? ;");
            ps.setString(1, str);
            ps.executeUpdate();
            st = con.createStatement();
            rs= st.executeQuery("select * from login");
            con.close();
            }catch(Exception e){ System.out.println(e);}
            request.setAttribute("resultset", rs);
            RequestDispatcher rd = request.getRequestDispatcher("userHome.jsp");
            rd.forward(request, response);
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
    }

}

它给出了这样的错误

HTTP Status 500 -

type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

org.apache.jasper.JasperException: javax.servlet.ServletException: java.sql.SQLException: Operation not allowed after ResultSet closed
  org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:502)
  org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:412)
  org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
  org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
  javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
  DeleteControl.doGet(DeleteControl.java:57)
  javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
  javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
root cause

javax.servlet.ServletException: java.sql.SQLException: Operation not allowed after ResultSet closed
  org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:865)

org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:794) org.apache.jsp.userHome_jsp._jspService(userHome_jsp.java:106) org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java: 70) javax.servlet.http.HttpServlet.service(HttpServlet.java:717) org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:388) org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet. java:313) org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260) javax.servlet.http.HttpServlet.service(HttpServlet.java:717) DeleteControl.doGet(DeleteControl.java:57) javax. servlet.http.HttpServlet.service(HttpServlet.java:617) javax.servlet.http.HttpServlet.service(HttpServlet.java:717) 根本原因

java.sql.SQLException:ResultSet 关闭后不允许操作 com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1078) com.mysql.jdbc.SQLError.createSQLException(SQLError.java:989) com.mysql.jdbc。 SQLError.createSQLException(SQLError.java:975) com.mysql.jdbc.SQLError.createSQLException(SQLError.java:920) com.mysql.jdbc.ResultSetImpl.checkClosed(ResultSetImpl.java:804) com.mysql.jdbc.ResultSetImpl。 next(ResultSetImpl.java:6986) org.apache.jsp.userHome_jsp._jspService(userHome_jsp.java:81) org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70) javax.servlet.http.HttpServlet。服务(HttpServlet.java:717)org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:388)org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)org.apache.jasper。小服务程序。JspServlet.service(JspServlet.java:260) javax.servlet.http.HttpServlet.service(HttpServlet.java:717) DeleteControl.doGet(DeleteControl.java:57) javax.servlet.http.HttpServlet.service(HttpServlet.java: 617) javax.servlet.http.HttpServlet.service(HttpServlet.java:717) 注意 Apache Tomcat/6.0.35 日志中提供了根本原因的完整堆栈跟踪。

Apache Tomcat/6.0.35

4

1 回答 1

0

你的代码是一团糟,你在这里创建了几个内存泄漏。使用DataSource像 Tomcat JDBC Pool 和 Apache Commmons DbUtils 这样的工厂来专注于您的问题。

此外,您不要conn.commit()在删除后执行 a 。您处于自动提交模式。你永远不应该在自动提交模式下工作。

您传递了一个已被连接关闭的结果集。

于 2013-07-26T08:12:26.097 回答