[英]Specified JDBC Driver com.mysql.jdbc.Driver class not found
I am using eclipse juno and hibernate 4.1.6 , mysql connector 5.1.24 , jboss 7.1.1 .我正在使用eclipse juno和hibernate 4.1.6 , mysql 连接器 5.1.24 , jboss 7.1.1 。
I tried this and this (as a module).我试过这个和 这个(作为一个模块)。
I have the connector JAR in WEB-INF/lib
folder, and I also tried once without it.我在
WEB-INF/lib
文件夹中有连接器 JAR,我也试过一次没有它。 Everything failed.一切都失败了。 I don't know anymore how I can solve this problem.
我不知道我该如何解决这个问题。 The whole stack trace:
整个堆栈跟踪:
18:35:44,284 ERROR [stderr] (http-localhost-127.0.0.1-8080-1) Error creating Session: org.hibernate.service.classloading.spi.ClassLoadingException: Specified JDBC Driver com.mysql.jdbc.Driver class not found
The connector is an the class path, it's in the Maven Dependencies Library.连接器是一个类路径,它在 Maven 依赖库中。 I put it in the lib folder and in the System Library...nothing works.
我把它放在 lib 文件夹和系统库中......没有任何效果。
public class HibernateUtil {
private static SessionFactory sessionFactory;
private static ServiceRegistry serviceRegistry;
static
{
try
{
Configuration configuration = new Configuration().configure("hibernate.cfg.xml");
serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
}
catch (HibernateException he)
{
System.err.println("Error creating Session: " + he);
throw new ExceptionInInitializerError(he);
}
}
Check in your hibernate.cfg.xml
file that you have correctly specified the driver class
and the dialect
:检查您已正确指定
driver class
和dialect
hibernate.cfg.xml
文件:
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
(these are properties of the <session-factory>
) (这些是
<session-factory>
属性)
Then, simply build your Session Factory:然后,只需构建您的会话工厂:
sessionFactory = new Configuration().configure().buildSessionFactory();
If the application still does not see your Maven dependency for the connector, try doing a clean install, and after it check in the list of libraries for your project that the MySQL Connector
appears there.如果应用程序仍然看不到连接器的 Maven 依赖项,请尝试进行全新安装,然后检查项目的库列表中是否出现
MySQL Connector
。
Please check your mysql connector version is compatible with mysql db installed on your system.请检查您的 mysql 连接器版本是否与系统上安装的 mysql db 兼容。 eg mysql connector version 5.1.26 is compatible with mysql 5.6.25 while 5.1.34 is not.
例如,mysql 连接器版本 5.1.26 与 mysql 5.6.25 兼容,而 5.1.34 则不兼容。
尝试将连接器添加到构建路径作为外部 jar 或,创建一个 lib 文件夹并将连接器 jar 放入其中,然后Add Library
到您的构建路径并提供该连接器 lib 文件夹路径。
Configuration configuration = new Configuration();
configuration.configure();
ServiceRegistryBuilder serviceRegistryBuilder = new ServiceRegistryBuilder().applySettings(configuration.getProperties());
sessionFactory = configuration.buildSessionFactory(serviceRegistryBuilder.buildServiceRegistry());
// end of static block
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.