Hibernate - ClassNotFoundException:com.mysql.jdbc.Driver [英] Hibernate - ClassNotFoundException: com.mysql.jdbc.Driver
问题描述
我试图通过Hibernate从MySQL数据库检索数据,但是我遇到了这个错误:无法创建sessionFactory object.org.hibernate.service.classloading.spi.ClassLoadingException:指定的JDBC驱动程序无法加载com.mysql.jdbc.Driver
java.lang.ClassNotFoundException:无法加载请求class:com.mysql.jdbc.Driver
[...]
我使用这个类名为DAOFactory来获得hibernate会话:
pre $ public $ DA $ a
$ b $ private static boolean isInstance =假;
private static SessionFactory sessionFactory;
private static ServiceRegistry serviceRegistry;
私有静态会话会话;
private DAOFactory()抛出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);
抛出新的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.0encoding =UTF-8?>
<!DOCTYPE hibernate-configuration PUBLIC - // Hibernate / Hibernate配置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>线程< / property>
< property name =cache.provider_class> org.hibernate.cache.NoCacheProvider< / property>
< property name =show_sql> true< / property>
< property name =hbm2ddl.auto>更新< / property>
< / session-factory>
< / hibernate-configuration>
和 mysql-connector-java-5.1.26-bin.jar
已经在类路径中:
有人看到我缺少的东西吗?
感谢Reimeus的回答。 mysql-connector-java-5.1.26-bin.jar
需要在运行时类路径中。
运行 - >运行配置... - >类路径 - >添加外部JAR。
清理所有内容,再试一次,异常消失。
I'm trying to retrieve data from a MySQL database through Hibernate, but I'm stuck with this error:
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
[...]
I use a class called DAOFactory to get the hibernate session:
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>
And mysql-connector-java-5.1.26-bin.jar
is already in the classpath:
Does anyone see what I'm missing ?
Thanks to Reimeus for the answer. mysql-connector-java-5.1.26-bin.jar
needs to be in the runtime classpath.
Run -> Run Configurations... -> Classpath -> Add external JAR.
Clean everything, try again, and the Exception is gone.
这篇关于Hibernate - ClassNotFoundException:com.mysql.jdbc.Driver的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!