0

I'm trying to connect a mysql base through DriverManager.getConnection and I'm not succeeding. I created a project in Eclipse, using java and the option of a web project - Dynamic Web Project. After several attempts, using DriverManager.getConnection(url, user, password) and debugging the project, I realized that when running the application through Run on Server or even Debug on Server, both cause an error for not finding the mysql Driver. the application as a desktop as a Java Application it recognizes the Driver and makes the connection. It doesn't show any errors.

Below is the connection class:

package br.com.util;

import java.sql.*;

public class MySqlConnection {

    private static final String url = "jdbc:mysql://localhost:3306/dbfinanceiro";
    private static final String user = "root";
    private static final String psw = "1234";
    
    public Connection getConnection() {
        
        Connection conn = null;
        
        try {
             conn = DriverManager.getConnection(url, user, psw);
            System.out.println("Driver MYSQL Conectado com Sucesso!");
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        return conn;
    }

}

INFORMATION: Server startup in [900] milliseconds
java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/dbfinanceiro
at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:706)
at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:229)
at br.com.util.MySqlConnection.getConnection(MySqlConnection.java:16)
at br.com.dao.UsuarioDao.validar(UsuarioDao.java:26)
at br.com.controller.Validarlogin.doPost(Validarlogin.java:34)
at jakarta.servlet.http.HttpServlet.service(HttpServlet.java:709)
at jakarta.servlet.http.HttpServlet.service(HttpServlet.java:792)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:223)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:158)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:185)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:158)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:197)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:97)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:542)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:119)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:690)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:78)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:356)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:399)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:870)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1762)`
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at org.apache.tomcat.util.threads.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1191)
at org.apache.tomcat.util.threads.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:659)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.base/java.lang.Thread.run(Thread.java:833)

I put the library in the project folder… right click and add the build path mysql-connector-j-8.0.32.jar.

When I run the project as a Java application. The same works correctly. Recognizes the getConnection and connects the database. Only when I run this same application as a web project does it give an error.

Progman
  • 16,827
  • 6
  • 33
  • 48
  • In a web application, where the driver is included in the WAR, you need to explicitly load the driver class with `Class.forName("com.mysql.cj.jdbc.Driver")` for it to be correctly registered with `DriverManager` (automatic driver loading happens only once, and usually only using the initial classpath, so the driver inside your WAR wasn't found and loaded automatically). Also, in a web application, you'd normally not use `DriverManager`, but you'd use a pre-configured `javax.sql.DataSource` backed by a connection pool. – Mark Rotteveel Apr 01 '23 at 09:25

0 Answers0