Issue
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");
}
}
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.
Solution
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:
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:
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
.
Answered By - CrazyCoder