0

I am trying to connect a mysql database to my java project, but I'm getting the error: java.lang.ClassNotFoundException: com.mysql.jdbc.Driver

As seen in the image, I added mysql-connector-java-5.1.48.jar to my project, but it still gives me the error.

public static Connection getConnection(){
        try {
            Class.forName("com.mysql.jdbc.Driver").newInstance();
            return DriverManager.getConnection(url, username, password);
        } catch (SQLException ex) {
            throw new RuntimeException("Error connecting to the database", ex);
        } catch (IllegalAccessException | ClassNotFoundException | InstantiationException e) {
            e.printStackTrace();
        }
        return null;}
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:582)
    at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:190)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:499)
    at java.base/java.lang.Class.forName0(Native Method)
    at java.base/java.lang.Class.forName(Class.java:291)
    at DatabaseAccess.DbConnection.getConnection(DbConnection.java:17)
    at DatabaseAccess.DbConnection.main(DbConnection.java:27)
Andronicus
  • 25,419
  • 17
  • 47
  • 88
Mapijs
  • 45
  • 7

1 Answers1

0

If you're using maven, add this to your <dependencies> tag:

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

for gradle:

compile group: 'mysql', name: 'mysql-connector-java', version: '8.0.19'
Andronicus
  • 25,419
  • 17
  • 47
  • 88
  • i use gradle, it seems to solve the problem. But now i got a different one. ```Caused by: java.sql.SQLException: The server time zone value 'West-Europa (standaardtijd)' is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the 'serverTimezone' configuration property) to use a more specifc time zone value if you want to utilize time zone support. ``` i looked at but don't understand how to change it https://stackoverflow.com/questions/26515700/mysql-jdbc-driver-5-1-33-time-zone-issue/44720416 – Mapijs Feb 07 '20 at 18:21
  • @Mapijs this is a different problem, you can post is as another question:) Since my answer resolved your issue, you might consider upvoting it / marking as correct;> – Andronicus Feb 07 '20 at 18:24