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: