Fala pessoal…
Sou novo em programação JAVA e por isso peço ajuda de vocês.
Estou fazendo uma aplicação JAVAWEB e estou tendo problemas na conexão com o banco de dados no SERVLET…
Segue o erro:
GRAVE: Servlet.service() for servlet [br.com.servlet.controller.UserLogar] in context with path [/SistemaJavaWeb] threw exception
java.lang.RuntimeException: java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
at br.com.factory.mysql.ConexaoMysql.getConnection(ConexaoMysql.java:26)
at br.com.DAO.UsuarioDAO.(UsuarioDAO.java:15)
at br.com.servlet.controller.UserLogar.doPost(UserLogar.java:42)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:110)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:492)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:165)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:104)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:1025)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:452)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1195)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:654)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:319)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1951)
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1794)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:264)
at br.com.factory.mysql.ConexaoMysql.getConnection(ConexaoMysql.java:20)
… 24 more
SEGUE O CóDIGO DO SERVLET:
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
// classe Login Sistemas
LoginSistema loginSis = new LoginSistema();
String userLogin = request.getParameter(“txtUser”);
String passLogin = request.getParameter(“txtPass”);
loginSis.setUser_login(userLogin);
loginSis.setPass_login(passLogin);
RequestDispatcher rd = null;
UsuarioDAO userDao = new UsuarioDAO();
if (userDao.verificarUsuario(loginSis)) {
HttpSession sessao = request.getSession();
sessao.setAttribute("sessaoUser", userLogin);
rd = request.getRequestDispatcher("/index-principal.jsp");
rd.forward(request, response);
} else {
request.setAttribute("mensagem", "Usuario ou senha inválidos");
rd = request.getRequestDispatcher("/index.jsp");
rd.forward(request, response);
}
}
SEGUE A CLASSE DE CONEXÃO:
package br.com.factory.mysql;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.management.RuntimeErrorException;
// ESSA CONEXÃO ESTÁ FUNCIONANDO COM O BANCO DE DADOS
public class ConexaoMysql {
private String URL = “jdbc:mysql://127.0.0.1/aulajpa”;
private String USER = “root”;
private String PASS = “root”;
private String DRIVER = “com.mysql.jdbc.Driver”;
public Connection getConnection() {
try {
Class.forName(DRIVER);
return DriverManager.getConnection(URL, USER, PASS);
} catch (SQLException erro) {
throw new RuntimeException(erro);
} catch (ClassNotFoundException erro) {
throw new RuntimeException(erro);
}
}
}
SEGUE O USUARIO DAO
package br.com.DAO;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import com.sun.istack.internal.logging.Logger;
import br.com.factory.mysql.ConexaoMysql;
import br.com.javaBean.model.LoginSistema;
public class UsuarioDAO {
private Connection conn;
public UsuarioDAO() {
this.conn = new ConexaoMysql().getConnection();
}
// método valida acesso
public boolean verificarUsuario(LoginSistema user) {
// if(this.user_login != null && this.pass_login != null) {
// if(this.user_login.equals("iso0406") && this.pass_login.equals("046284"))
// return true;
// }
String sql = "SELECT * FROM login WHERE login=? AND senha=?";
try {
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, user.getUser_login());
ps.setString(2, user.getPass_login());
ResultSet rs = ps.executeQuery();
if (rs.next())
return true;
} catch (SQLException erro) {
Logger.getLogger(UsuarioDAO.class.getName(), null).log(Level.SEVERE, null, erro);
}
return false;
}