Hibernate 找不到指定的JDBC驱动程序com.ibm.db2.jcc.DB2Driver类

Hibernate 找不到指定的JDBC驱动程序com.ibm.db2.jcc.DB2Driver类,hibernate,jdbc,db2,classnotfoundexception,Hibernate,Jdbc,Db2,Classnotfoundexception,我花了相当多的时间进行研究,试图找出我的配置哪里出了问题,但我完全被卡住了。从我阅读的帖子中可以看出,我的驱动程序不在类路径中。我添加了Class.forName(“com.ibm.db2.jcc.DB2Driver”)行,以确保驱动程序可用并在该行中加载。如果能给我一些建议,我会非常感激 我的代码是: public class TestHibernateConfig { private static SessionFactory sessionFactory; pu

我花了相当多的时间进行研究,试图找出我的配置哪里出了问题,但我完全被卡住了。从我阅读的帖子中可以看出,我的驱动程序不在类路径中。我添加了Class.forName(“com.ibm.db2.jcc.DB2Driver”)行,以确保驱动程序可用并在该行中加载。如果能给我一些建议,我会非常感激

我的代码是:

    public class TestHibernateConfig {

    private static SessionFactory sessionFactory;

    public static void testConfig() {
        try {
            DB2Driver drvr = (DB2Driver) Class.forName("com.ibm.db2.jcc.DB2Driver").newInstance();

            Configuration cfg = new Configuration();
            cfg.configure("hibernate.cfg.xml");
            sessionFactory = cfg.buildSessionFactory();
            Session sess = sessionFactory.getCurrentSession();
            Transaction tx = sess.beginTransaction();

            Criteria crit = sess.createCriteria(ItemData.class);
            List items = crit.list();

        } catch (Exception e) {
            Logger logger = Logger.getLogger("");
            logger.error(e.getMessage());
        }
    }
}
当我的代码命中时,我得到类not found错误: sessionFactory=cfg.buildSessionFactory()

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="speed2db2">
 <property name="hibernate.dialect">org.hibernate.dialect.DB2Dialect</property>
  <property name="hibernate.connection.driver_class">com.ibm.db2.jcc.DB2Driver</property>
  <property name="hibernate.connection.url">jdbc:db2://appdb:50000/MYTEST</property>
  <property name="hibernate.connection.username">myUser</property>
  <property name="hibernate.connection.password">myPass</property>
 </session-factory>
</hibernate-configuration>
错误发生在这一行

 sessionFactory = cfg.buildSessionFactory();
部署到my.war文件:
.战争
|-WEB-INF
||-lib
|||-db2jcc4.jar
|||-hibernate-core-4.1.2.Final.jar
|||-hibernate-entitymanager-4.1.2.Final.jar

|||-hibernate-jpa-2.0-api-1.0.1.Final.jar

我想可能是因为hibernate.cfg.xml放错地方了。您应该检查它是否在类路径中,或者您可以像这样尝试在方法调用中添加类路径

我想我没有提到我正在使用JBoss7.1。最后,我无法理解为什么Hibernate无法加载驱动程序,但通过切换到使用JNDI并在JBoss standalone.xml中配置数据源,而不是尝试从Hibernate建立连接,解决了这一问题。

您从何处得到错误,是否可以共享相关的堆栈跟踪,还是其他形式的日志?我添加了堆栈跟踪。它确实显示配置文件正在加载。感谢您的响应。我确实尝试过移动我的配置文件,并用您显示的路径引用它。我也有同样的问题。您可以在上面的编辑中看到它正在查找配置文件。我已经确认db2jcc4.jar位于服务器上my.war的lib文件夹中。另外,如上所述,类.forName()正确加载驱动程序。我还确认了网址是正确的。
SessionFactory sessionFactory = new Configuration().configure(
                    "/com/mkyong/persistence/hibernate.cfg.xml")
                    .buildSessionFactory();
 sessionFactory = cfg.buildSessionFactory();
SessionFactory sessionFactory = new Configuration().configure(
                    "/com/mkyong/persistence/hibernate.cfg.xml")
                    .buildSessionFactory();