0

I am new to Jakarta EE, I have seen some other similar questions, I have followed them but can't seem to see what I am doing wrong as I have followed all those.

Seems like the class com.mysql.jdbc.Driver is not found. Im banging my head on the wall trying to find out why

I am learning JPA with hibernate, and cant seem to connect to my localhost from MySql.

I have added the dependency in the pom.xml:

    <?xml version="1.0" encoding="UTF-8"?>
    <project 
        xmlns="http://maven.apache.org/POM/4.0.0"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation=
        "http://maven.apache.org/POM/4.0.0
        http://maven.apache.org/xsd/maven-4.0.0.xsd"
    >
        <modelVersion>4.0.0</modelVersion>
    
        <groupId>com.example</groupId>
        <artifactId>Jakarta2</artifactId>
        <version>1.0-SNAPSHOT</version>
        <name>Jakarta2</name>
        <packaging>war</packaging>
    
        <properties>
            <maven.compiler.target>1.8</maven.compiler.target>
            <maven.compiler.source>1.8</maven.compiler.source>
            <junit.version>5.7.0</junit.version>
        </properties>
    
        <dependencies>
            <dependency>
                <groupId>javax.ejb</groupId>
                <artifactId>javax.ejb-api</artifactId>
                <version>3.2.2</version>
                <scope>provided</scope>
            </dependency>
            <dependency>
                <groupId>javax.ws.rs</groupId>
                <artifactId>javax.ws.rs-api</artifactId>
                <version>2.1.1</version>
                <scope>provided</scope>
            </dependency>
            <dependency>
                <groupId>javax.servlet</groupId>
                <artifactId>javax.servlet-api</artifactId>
                <version>4.0.1</version>
                <scope>provided</scope>
            </dependency>
            <dependency>
                <groupId>org.hibernate</groupId>
                <artifactId>hibernate-core</artifactId>
                <version>5.4.25.Final</version>
            </dependency>
            <dependency>
                <groupId>org.junit.jupiter</groupId>
                <artifactId>junit-jupiter-api</artifactId>
                <version>${junit.version}</version>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>org.junit.jupiter</groupId>
                <artifactId>junit-jupiter-engine</artifactId>
                <version>${junit.version}</version>
                <scope>test</scope>
            </dependency>
            <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java-->
    
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>8.0.23</version>
            </dependency>
    
        </dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-war-plugin</artifactId>
                    <version>3.3.0</version>
                </plugin>
            </plugins>
        </build>
    </project>

persistence.xml:

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <persistence 
    xmlns="http://xmlns.jcp.org/xml/ns/persistence"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence                 
    http://xmlns.jcp.org/xml/ns/persistence/persistence_2_2.xsd"
    version="2.2">
    <persistence-unit name="MyFirstEntityManager" transaction-type="RESOURCE_LOCAL">
        <properties>
            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL8Dialect"/>
            <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
            <-- also tried com.mysql.cj.jdbc.Driver (made no difference) -->
            <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost/Test"/>
            <property name="javax.persistence.jdbc.username" value="root"/>
            <property name="javax.persistence.jdbc.password" value="root"/>
        </properties>
    </persistence-unit>
    </persistence>

AnimalsServlet.java:

    package animals;
    
    import javax.persistence.Entity;
    import javax.persistence.EntityManagerFactory;
    import javax.persistence.Persistence;
    import javax.servlet.*;
    import javax.servlet.http.*;
    import javax.servlet.annotation.*;
    import java.io.IOException;
    import java.io.PrintWriter;
    
    import myServices.HtmlHandler;
    
    @WebServlet("/animals")
    public class AnimalsServlet extends HttpServlet {

        @Override
        protected void doGet(request, response) throws ServletException, IOException 
        {
    
            EntityManagerFactory emf =      
            Persistence.createEntityManagerFactory("MyFirstEntityManager");
    
            
        }
    
    }        

This is the project structure where I have added the library from maven

Here I can see the mysql connector added to the external libraries folder

Exception thrown on web browser

Error on the server

Library added to project structure

At this point I have given up, Java seems unnecessarily complicated. I have looked at so many resources and could not see what I was doing wrong, so I decided to ask a question, I apologise in advance if it is a repeat, It's just that I have followed most.

Small Edit: If it is easier to connect via Data-source or some other way, I am open to that too, but I would prefer to do it programmatically if possible.

CrazyCoder
  • 389,263
  • 172
  • 990
  • 904
ptmp_727
  • 65
  • 9

2 Answers2

1

The issue is with the persistence.xml file:

<property name="javax.persistence.jdbc.username" value="root"/>

This property is not correct and you are not setting the user name which causes the following exception:

Caused by: java.sql.SQLException: Access denied for user ''@'localhost' (using password: YES)

The solution is to use the correct javax.persistence.jdbc.user property:

<property name="javax.persistence.jdbc.user" value="root"/>

That is why it's important to provide the full error when asking questions on StackOverflow instead of the partial screenshots.


The second possible problem is the missing JDBC Driver. In this case the error would be different:

Caused by: java.lang.ClassNotFoundException: Could not load requested class : com.mysql.jdbc.Driver

If you have the issue with the MySQL JDBC Driver class not found, make sure the jar is included into the artifact that you are deploying:

artifact

If you update pom.xml with this dependency:

          <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>8.0.23</version>
          </dependency>

Then delete artifacts in IntelliJ IDEA and reimport the project, the new artifacts will be created automatically with the JDBC driver already added:

JDBC added

Note that IDE doesn't update artifacts on Maven reimport, it's a known limitation. That is why it's important do delete the existing artifacts first and reimport the project from Maven to re-generate them with the up-to-date dependencies from pom.xml.

CrazyCoder
  • 389,263
  • 172
  • 990
  • 904
  • I added it, now its under the lib folder, but Im getting the same error, I sent a couple of emails, I also updated the persistence.xml – ptmp_727 Mar 02 '21 at 20:24
  • No, you edited the wrong artifact, please read the complete answer and follow all the steps exactly as described. Edit pom.xml to include JDBC driver dependency, delete artifacts in the IDE, reimport the Maven project so that IDE generates the new artifacts with the JDBC driver added. Restart the server so that the new artifacts are deployed. I've triple tested it with the samele project and it works perfectly. – CrazyCoder Mar 02 '21 at 20:29
0

I assume you are deploying to Glassfish? Please note that the default implementation in Glassfish is EclipseLink and it comes bundled with that already. Java/Jakarta EE apps usually only need a dependency to the API artifacts e.g. jakartaee-api:

<dependency>
  <groupId>jakarta.platform</groupId>
  <artifactId>jakarta.jakartaee-api</artifactId>
  <version>8.0.0</version>
  <scope>provided</scope>
</dependency>

Going against the default implementations might cause troubles because this is an integration effort that isn't always that easy to achieve.

Did you check if the WAR file that is deployed to Glassfish contains the mysql jdbc JAR in WEB-INF/lib? Anyway, I would recommend you setup a datasource in Glassfish and use the JNDI name in the persistence.xml instead as an application server provides connection pooling and transaction integration with the datasource out of the box.

Christian Beikov
  • 15,141
  • 2
  • 32
  • 58
  • Hello, Im so sorry I should have mentioned, I am using JBoss server. – ptmp_727 Mar 02 '21 at 11:35
  • In that case, the part about just using the Java/Jakarta EE API artifacts with the provided scope applies as well. The server comes with these dependencies out of the box. – Christian Beikov Mar 02 '21 at 15:24
  • Im just learning this, so please accept my apologies Im not too familiar with this. When you mean check the jar files in the WEB-INF folder, you mean in the target directory? – ptmp_727 Mar 02 '21 at 17:33
  • Also IntelliJ does not seem to pick up the Jakarta namespace, Does adding this dependency you suggested incorporate the namespace? – ptmp_727 Mar 02 '21 at 17:34