Art*_*hur 18 java mysql hibernate jdbc
我正在尝试通过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
[...]
Run Code Online (Sandbox Code Playgroud)
我使用一个名为DAOFactory的类来获取hibernate会话:
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 ;
}
}
Run Code Online (Sandbox Code Playgroud)
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>
Run Code Online (Sandbox Code Playgroud)
并且mysql-connector-java-5.1.26-bin.jar已经在类路径中:

有谁看到我错过了什么?
Art*_*hur 28
感谢Reimeus的回答.mysql-connector-java-5.1.26-bin.jar需要在运行时类路径中.
运行 - >运行配置... - >类路径 - >添加外部JAR.
清理所有内容,再试一次,Exception就不见了.
ROM*_*eer 19
对于那些使用Maven的人:在pom.xml中添加以下依赖项.
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.17</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
或者从这里选择其他版本.
然后你可以使用以下方法获取工件:
mvn dependency:resolve
Run Code Online (Sandbox Code Playgroud)
(如果您不使用IDE).
面对同样的问题mysql-connector-java-5.1.48-bin.jar.为了解决这个问题,我改变了驱动程序类名称
<property name="connection.driver_class">com.mysql.cj.jdbc.Driver</property>
Run Code Online (Sandbox Code Playgroud)
到
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
55168 次 |
| 最近记录: |