【问题标题】:Hibernate - ClassNotFoundException: com.mysql.jdbc.Driver休眠 - ClassNotFoundException:com.mysql.jdbc.Driver
【发布时间】:2013-10-19 15:02:19
【问题描述】:

我正在尝试通过 Hibernate 从 MySQL 数据库中检索数据,但我遇到了这个错误:

Failed to create sessionFactory object.org.hibernate.service.classloading.spi.ClassLoadingException: Specified JDBC Driver com.mysql.jdbc.Driver could not be loaded

java.lang.ClassNotFoundException: Could not load requested class : com.mysql.jdbc.Driver
[...]

我使用一个名为 DAOFactory 的类来获取休眠会话:

public class DAOFactory {

    private static boolean isInstance = false;  
    private static SessionFactory sessionFactory;
    private static ServiceRegistry serviceRegistry; 
    private static Session session;

    private DAOFactory() throws ExceptionInInitializerError{        
        if( !isInstance ) {
            try {               
                Configuration cfg   = new Configuration().configure();              
                serviceRegistry     = new ServiceRegistryBuilder().applySettings(cfg.getProperties())
                                                .buildServiceRegistry();
                sessionFactory      = cfg.buildSessionFactory(serviceRegistry);
            } catch (Throwable ex) {
                System.err.println("Failed to create sessionFactory object."+ ex);
                throw new ExceptionInInitializerError(ex);
            }
            session = sessionFactory.openSession();         
            isInstance = true ;
        }               
    }

    public static DAOFactory getInstance() {        
        return new DAOFactory() ;
    }

    public Session getSession() {
        return session ;
    }
}

hibernate.cfg.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
                                         "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory name="">
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.url">jdbc:mysql://localhost:3306/enigma</property>
        <property name="connection.username">root</property>
        <property name="connection.password"></property>
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="connection.pool_size">1</property>
        <property name="current_session_context_class">thread</property>
        <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
        <property name="show_sql">true</property>
        <property name="hbm2ddl.auto">update</property>
    </session-factory>
</hibernate-configuration>

并且mysql-connector-java-5.1.26-bin.jar 已经在类路径中:

有人看到我错过了什么吗?

【问题讨论】:

标签: java mysql hibernate jdbc


【解决方案1】:

感谢 Reimeus 的回答。 mysql-connector-java-5.1.26-bin.jar 需要在 runtime 类路径中。

运行 -> 运行配置... -> 类路径 -> 添加外部 JAR。

清理一切,再试一次,异常就消失了。

【讨论】:

    【解决方案2】:

    对于使用Maven的人:在 pom.xml 中添加以下依赖项。

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

    或从here中选择另一个版本。

    然后你可以使用:

    mvn dependency:resolve
    

    (如果您不使用 IDE)。

    【讨论】:

    【解决方案3】:

    遇到与mysql-connector-java-5.1.48-bin.jar. 相同的问题为了解决此问题,我将驱动程序类名称从

    <property name="connection.driver_class">com.mysql.cj.jdbc.Driver</property>
    

    <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
    

    【讨论】:

      【解决方案4】:

      在某些情况下,通过Run -> Run Configurations... -> Classpath -> Add external JARjar 添加到classpath 可能不是一个合适的解决方案。

      第一种情况

      jar 文件无法放入classpath 文件夹时,有另一种方法可以从其他地方加载类。您只需要实例化URLClassLoader,然后在其上调用loadClass()(曾提到here):

      URLClassLoader urlCL = new URLClassLoader(new URL[] {"path_to_jar"});
      Class driverClass = urlCL.loadClass("com.mysql.jdbc.Driver");
      

      第二种情况

      如果你想在运行时将你的类添加到classpath(我更喜欢Ranjit Aneeshhere的答案),为此你可以创建一个非常简单的自定义类加载器扩展URLClassLoader重写 addUrl 方法:

      public class DynamicURLClassLoader extends URLClassLoader {
      
          public DynamicURLClassLoader(URLClassLoader classLoader) {
              super(classLoader.getURLs());
          }
      
          @Override
          public void addURL(URL url) {
              super.addURL(url);
          }
      }
      

      然后调用它:

      URLClassLoader urlCL = (URLClassLoader) ClassLoader.getSystemClassLoader();
      new DynamicURLClassLoader(urlCL).addURL("path_to_jar");
      

      【讨论】:

        【解决方案5】:

        在使用 mysql-connector-java-5.1.48-bin.jar 时遇到了同样的问题。为了解决这个问题,我将驱动程序类名称从

        com.mysql.cj.jdbc.Driver 到

        com.mysql.jdbc.Driver

        【讨论】:

          【解决方案6】:
          <dependency>
              <groupId>mysql</groupId>
              <artifactId>mysql-connector-java</artifactId>
              <version>8.0.15</version>
              <scope>provided</scope> 
          </dependency>
          
          • 我在 Maven 中有以上依赖。
          • 范围标签导致了错误。
          • 删除范围标签解决了这个问题。

          【讨论】:

            【解决方案7】:

            下载mysql-connector-java-8.0.20.jar 来自https://repo1.maven.org/maven2/mysql/mysql-connector-java/8.0.20/ 将jar添加到类路径

            运行 -> 运行配置... -> 类路径 -> 添加外部 JAR。

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 2011-12-16
              • 2012-02-03
              • 2021-10-21
              • 2015-02-08
              • 2019-04-18
              • 2017-02-06
              • 2017-05-18
              相关资源
              最近更新 更多