0
LoginDAO.java:
public class LoginDAO {
private static String sql = "select * from login where login=? and senha=?";
private static String url = "jdbc:mysql://localhost:3306/escola";
private static String username = "root";
private static String password = "";
public boolean conectar (String login, String senha) {
try {
Class.forName("com.mysql.jdbc.driver");
Connection connection = DriverManager.getConnection(url, username, password);
PreparedStatement st = connection.prepareStatement(sql);
st.setString(1, login);
st.setString(2, senha);
ResultSet resp = st.executeQuery();
if (resp.next()) {
return true;
}
} catch (Exception e) {
throw new RuntimeException ("Erro ao conectar com o banco de dados!");
}
return false;
}
}
Login.java:
@Override
public String execute(HttpServletRequest request, HttpServletResponse response) {
// TODO Auto-generated method stub
String login = request.getParameter("login");
String senha = request.getParameter("senha");
LoginDAO dao = new LoginDAO();
if (dao.conectar(login, senha)) {
HttpSession session = request.getSession();
session.setAttribute("login", login);
return "index.html";
} else {
return "sobre.html";
}
}
The archive mysql-Connector-java-5.1.48-bin. jar was added to the folder referenced Libraries when I put in the build path.
HTTP Status 500 - Internal Server Error Type Exception Report
Message: Error connecting to database!
Description The server encountered an Unexpected condition that prevented it from fulfilling the request.
Exception
java.lang.Runtimeexception: Error connecting to database! br.com.lerolero.dao.Logindao.conectar(Logindao.java:26) br.com.lerolero.web.Login.execute(Login.java:34) br.com.lerolero.web.Controller.service(Controller.java:37) javax.servlet.http.HttpServlet.service(Httpservlet.java:741) org.apache.Tomcat.websocket.server.WsFilter.doFilter(Wsfilter.java:53)
I managed to fix it. I changed the catch and to "Driver" how you spoke and found out that you were giving a Classnotfoundexception because the file .jar jdbc has to be inside the folder lib in the directory WEB-INF and not in the referenced Libraries, where it was the eclipse itself that added the . jar to the referenced Libraries. Thanks!
– Rafa Marques