1

To add it I tried sevral methods that I found online but none of them worked for me.

Here is how I try to import the driver:

1- I tried the method in the answer : Correct way to add external jars (lib/*.jar) to an IntelliJ IDEA project

2- I go to File ==> Project Structure==> Libraries then I add the mysql connecter .jar

Here is my Java class for the connection :

package dao;

import java.sql.Connection;
import java.sql.DriverManager;

public class DbConnection {
    private static Connection connection;


    static {
        try {
            Class.forName("com.mysql.jdbc.Driver");
            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/supermarche", "root","password");

        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

        public static Connection getConnection() {
            return connection;
        }



}

my Servlet

    package com.marcheli.shoping;
    
    import jakarta.servlet.*;
    import jakarta.servlet.http.*;
    import user.Client;
    import user.ManagementUser;
    
    import java.io.IOException;
    
    public class main extends HttpServlet {
        @Override
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            ManagementUser manage = new ManagementUser();
//the class ManagementUser use the getConnection() method and performe operation in DB
    



            Client c = manage.signUp(new Client("h","12334","email@sj",
                    "072737","ksk","jd"));



            request.getRequestDispatcher("index.jsp").forward(request, response);
    
    
        }
    

    }

In my web Browser, When I visit the URL for this Servlet I get this errors(it tells me the JDBC is not found):

java.lang.ExceptionInInitializerError user.ManagementUser.signUp(ManagementUser.java:14) com.marcheli.shoping.main.doGet(main.java:18) jakarta.servlet.http.HttpServlet.service(HttpServlet.java:683) jakarta.servlet.http.HttpServlet.service(HttpServlet.java:792) org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)

cause mère

java.lang.RuntimeException: java.lang.ClassNotFoundException: com.mysql.jdbc.Driver dao.DbConnection.(DbConnection.java:16) user.ManagementUser.signUp(ManagementUser.java:14) com.marcheli.shoping.main.doGet(main.java:18) jakarta.servlet.http.HttpServlet.service(HttpServlet.java:683) jakarta.servlet.http.HttpServlet.service(HttpServlet.java:792) org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)

EDIT I tried also to add the connector to my Artifact:

Modules

Artifacts

HamzaDevXX
  • 154
  • 1
  • 10
  • 1
    Apart from the concrete question, there are at least 2 serious problems visible in your coding attempt considering that you wanted to run it in a multi-threaded environment: 1) a `static Connection` is subject to resource leaking and data integrity problems. 2) using `DriverManager` plain vanilla is not the correct approach if you want to keep it performant, you should be using server-provided connection pooling facility. To fix 1) carefully read https://stackoverflow.com/a/9431863 and to improve 2) carefully read https://stackoverflow.com/a/2299870 – BalusC Jan 24 '23 at 18:29
  • @BalusC thank you for your comment, Actually I'm new to java developpement and I'm following a tuto on Youtube and the guy chose this approach. When I'm gonna learn the basics I'll start learning about the performance and the best practice. – HamzaDevXX Jan 24 '23 at 18:34
  • 2
    Ew. Good luck. Consider using an authoritative tutorial/book instead of watching video of a random amateur on Internet. You'll otherwise have a lot to clean up and/or XY-problems afterwards. – BalusC Jan 24 '23 at 18:36
  • 1
    Does https://stackoverflow.com/a/51698676/104891 help? – CrazyCoder Jan 24 '23 at 18:39
  • @CrazyCoder I went to `Project Structure`then `Artifacts` I found the mysql connector .jar on the column `Available Elements`I double clicked it then I saw I the connector moved to a directory lib inside WEB-INF (lib directory has been created after the double click). I satarted the server and now I get an error that says `Can't copy (Path of the the connector 'located in my downloads') to (Path of WEB-INF/lib 'the full path'). – HamzaDevXX Jan 24 '23 at 19:18
  • Make sure the library exists and the artifact is configured correctly. It should work. If it fails, share a sample project on GitHub. – CrazyCoder Jan 24 '23 at 19:19
  • @BalusC thank you, Do you have any recommendations ? – HamzaDevXX Jan 24 '23 at 19:19
  • @CrazyCoder Should I place the the library inside the project before attempting to including it in `Artificats` and Modules ? – HamzaDevXX Jan 24 '23 at 19:21
  • 1
    It should not matter how you configure the library. If it's present in the artifact dialog, it should work. Note that if you are using Maven or Gradle, dependencies should be specified in the build file instead (pom.xml or build.gradle). IDE will generate the artifacts automatically in this case. – CrazyCoder Jan 24 '23 at 19:23
  • @CrazyCoder Can you please check the pictures that I have added. – HamzaDevXX Jan 25 '23 at 11:33
  • 1
    The issue is that you add dependencies manually to a project that is managed by the external build system (Maven or Gradle). The dependencies must be added in the build file instead. – CrazyCoder Jan 25 '23 at 15:05
  • @CrazyCoder Yes, this was the problem. Thank you. – HamzaDevXX Mar 11 '23 at 22:08

0 Answers0