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

1. Error reporting issues

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

2. Problem background

Create a new Java project, add Hibernate framework support, start the test (run the main() method in the default Main class), and an error occurs.

Main.java

import org.hibernate.HibernateException;
import org.hibernate.Metamodel;
import org.hibernate.query.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

import javax.persistence.metamodel.EntityType;

import java.util.Map;

public class Main {
    
    
    private static final SessionFactory ourSessionFactory;

    static {
    
    
        try {
    
    
            Configuration configuration = new Configuration();
            configuration.configure();

            ourSessionFactory = configuration.buildSessionFactory();
        } catch (Throwable ex) {
    
    
            throw new ExceptionInInitializerError(ex);
        }
    }

    public static Session getSession() throws HibernateException {
    
    
        return ourSessionFactory.openSession();
    }

    public static void main(final String[] args) throws Exception {
    
    
        final Session session = getSession();
        try {
    
    
            System.out.println("querying all the managed entities...");
            final Metamodel metamodel = session.getSessionFactory().getMetamodel();
            for (EntityType<?> entityType : metamodel.getEntities()) {
    
    
                final String entityName = entityType.getName();
                final Query query = session.createQuery("from " + entityName);
                System.out.println("executing: " + query.getQueryString());
                for (Object o : query.list()) {
    
    
                    System.out.println("  " + o);
                }
            }
        } finally {
    
    
            session.close();
        }
    }
}

The project structure is shown below:
Insert image description here

3. Cause analysis

The driver package for the corresponding version of mysql is missing.

4. Solution

Step 1: Go to the official website to download the corresponding version of the driver, and then unzip it to get the jar package (this article takes downloading version 5.1.47 as an example)

Official website to download the corresponding version of the driver (jar package) address: https://downloads.mysql.com/archives/cj/

Insert image description here

Downloaded file: mysql-connector-java-5.1.47.zip
Insert image description here

Unzip the file and get the jar package: mysql-connector-java-5.1.47.jar

Insert image description here

Step 2: Import the jar package into the project

Insert image description here

Insert image description here

Select mysql-connector-java-5.1.47.jar, as shown in the figure below.
Insert image description here

After adding mysql-connector-java-5.1.47.jar, it is as shown in the figure below. Then click the "OK" button.
Insert image description here

Guess you like

Origin blog.csdn.net/Shipley_Leo/article/details/130784834