Bom dia Pessoal, sou iniciante e estou acompanhando a apostila fj21 da Caelum, estou com um erro HTTP Status 500 – Internal Server Error Request processing failed; nested exception is java.lang.RuntimeException: java.sql.SQLException: Operation not allowed after ResultSet closed, fiz algumas pesquisas e não achei nada que possa me ajudar na resolução.
Seguem os códigos:
usuarioDAO.java
package br.com.caelum.tarefas.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import br.com.caelum.tarefas.jdbc.ConnectionFactory;
import br.com.caelum.tarefas.modelo.Usuario;
public class UsuarioDAO {
Connection connection;
public UsuarioDAO() {
this.connection = new ConnectionFactory().getConnection();
}
public boolean existeUsuario(Usuario usuario) {
String sql = "select * from usuarios where login= ? and senha= ?";
try {
PreparedStatement stmt = this.connection.prepareStatement(sql);
stmt.setString(1, usuario.getUsuario());
stmt.setString(2, usuario.getSenha());
ResultSet rs = stmt.executeQuery();
stmt.execute();
// verifica se existe retorno na consulta
if (rs.next()) {
stmt.close();
return true;
} else {
stmt.close();
return false;
}
}
catch (SQLException e) {
throw new RuntimeException(e);
}
}
}
formulario-login.jsp
<%@ page language=“java” contentType="text/html; charset=UTF-8"
pageEncoding=“UTF-8”%>
Página de Login das Tarefas
Login:Senha:
Usuario.java
package br.com.caelum.tarefas.modelo;
public class Usuario {
private String usuario;
private String senha;
public String getUsuario() {
return usuario;
}
public void setUsuario(String usuario) {
this.usuario = usuario;
}
public String getSenha() {
return senha;
}
public void setSenha(String senha) {
this.senha = senha;
}
}
ConnectionFactory.java
package br.com.caelum.tarefas.jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class ConnectionFactory {
public Connection getConnection(){
System.out.println("Conectando ao Banco de Dados...");
String stringDeConexao="jdbc:mysql://localhost:3306/fj21";
String user="root";
String pass="senha";
try{
DriverManager.registerDriver(new com.mysql.jdbc.Driver());
return DriverManager.getConnection
(stringDeConexao,user,pass);
}catch(SQLException e){
throw new RuntimeException(e+"\nNAO FOI POSSIVEL CONECTAR NO BANCO DE DADOS");
}
}
}
LoginController.java
package br.com.caelum.tarefas.controller;
import javax.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import br.com.caelum.tarefas.dao.UsuarioDAO;
import br.com.caelum.tarefas.modelo.Usuario;
@Controller
public class LoginController {
@RequestMapping("loginForm")
public String loginForm() {
return "formulario-login";
}
@RequestMapping("efetuaLogin")
public String efetuaLogin(Usuario usuario, HttpSession session) {
if (new UsuarioDAO().existeUsuario(usuario)) {
session.setAttribute("usuarioLogado", usuario);
return "menu";
}
return "redirect:loginForm";
}
}
O erro que ocorre é esse!
HTTP Status 500 – Internal Server Error
Type Exception Report
Message Request processing failed; nested exception is java.lang.RuntimeException: java.sql.SQLException: Operation not allowed after ResultSet closed
Description The server encountered an unexpected condition that prevented it from fulfilling the request.
Exception
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.RuntimeException: java.sql.SQLException: Operation not allowed after ResultSet closed
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:982)
org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:872)
javax.servlet.http.HttpServlet.service(HttpServlet.java:661)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
Root Cause
java.lang.RuntimeException: java.sql.SQLException: Operation not allowed after ResultSet closed
br.com.caelum.tarefas.dao.UsuarioDAO.existeUsuario(UsuarioDAO.java:45)
br.com.caelum.tarefas.controller.LoginController.efetuaLogin(LoginController.java:21)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.lang.reflect.Method.invoke(Method.java:498)
org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:205)
org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:133)
org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:97)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738)
org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:967)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:901)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:872)
javax.servlet.http.HttpServlet.service(HttpServlet.java:661)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
Root Cause
java.sql.SQLException: Operation not allowed after ResultSet closed
com.mysql.jdbc.SQLError.createSQLException(SQLError.java:964)
com.mysql.jdbc.SQLError.createSQLException(SQLError.java:897)
com.mysql.jdbc.SQLError.createSQLException(SQLError.java:886)
com.mysql.jdbc.SQLError.createSQLException(SQLError.java:860)
com.mysql.jdbc.ResultSetImpl.checkClosed(ResultSetImpl.java:743)
com.mysql.jdbc.ResultSetImpl.next(ResultSetImpl.java:6285)
br.com.caelum.tarefas.dao.UsuarioDAO.existeUsuario(UsuarioDAO.java:34)
br.com.caelum.tarefas.controller.LoginController.efetuaLogin(LoginController.java:21)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.lang.reflect.Method.invoke(Method.java:498)
org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:205)
org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:133)
org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:97)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738)
org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:967)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:901)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:872)
javax.servlet.http.HttpServlet.service(HttpServlet.java:661)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
Note The full stack trace of the root cause is available in the server logs.
Estou usando o tomcat 8.5.2 no eclipse