删除条目并将结果集从servlet发送到jsp页面时出错

时间:2013-07-26 08:02:22

标签: jsp tomcat servlets

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)root   原因

     

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.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)注意   Apache中提供了根本原因的完整堆栈跟踪   Tomcat / 6.0.35日志。

     

Apache Tomcat / 6.0.35

1 个答案:

答案 0 :(得分:0)

你的代码是一个糟糕的混乱,你在这里创建了几个内存泄漏。使用DataSource工厂(如Tomcat JDBC Pool和Apache Commmons DbUtils)来关注您的问题。

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

您传递的结果已被连接关闭。