2012-04-06 48 views
0

我花了相當多的時間研究並試圖弄清楚我的配置出了問題,但完全停滯不前。從我一直在閱讀的帖子的含義是,我的驅動程序不在類路徑中。我添加了Class.forName(「com.ibm.db2.jcc.DB2Driver」)行,以確保驅動程序可用並在該行中加載。我真的很感激任何關於我可能錯過的建議。指定的JDBC驅動程序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()); 
     } 
    } 
} 

我得到的類未找到錯誤,當我的代碼點擊: 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> 

的persistence.xml

<?xml version="1.0" encoding="UTF-8"?> 
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"> 
    <persistence-unit name="speedPersistUnit"> 
     <class>ItemData</class> 
     <properties> 
      <property name="hibernate.dialect" value="org.hibernate.dialect.DB2Dialect"/> 
      <property name="hibernate.connection.url" value="jdbc:db2://appdb:50000/MYTEST"/> 
      <property name="hibernate.default_schema" value="MYSCHEMA"/> 
      <property name="hibernate.connection.username" value="myUser"/> 
      <property name="hibernate.connection.password" value="myPass"/> 
      <property name="hibernate.connection.driver_class" value="com.ibm.db2.jcc.DB2Driver"/> 
     </properties> 
    </persistence-unit> 
</persistence> 

在Eclipse中我能夠連接到數據庫,如果我去Hibernate的角度來看,我能夠瀏覽數據庫模式。我也可以使用HQL編輯器在我的課堂上加載數據。

編輯:

09:35:50,338 INFO [org.hibernate.cfg.Configuration] (http-xxxxxx-xx.xx.xx.xxxx-15081-1) HHH000043: Configuring from resource: /com/newpig/speed2DB2/hibernate.cfg.xml 
09:35:50,340 INFO [org.hibernate.cfg.Configuration] (http-xxxxxx-xx.xx.xx.xxxx-15081-1) HHH000040: Configuration resource: /com/newpig/speed2DB2/hibernate.cfg.xml 
09:35:50,369 INFO [org.hibernate.cfg.Configuration] (http-xxxxxx-xx.xx.xx.xxxx-15081-1) HHH000041: Configured SessionFactory: speed2db2 
09:35:52,059 INFO [org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl] (http-xxxxxx-xx.xx.xx.xxxx-15081-1) HHH000402: Using Hibernate built-in connection pool (not for production use!) 
09:36:16,856 ERROR [] (http-xxxxxx-xx.xx.xx.xxxx-15081-1) Specified JDBC Driver com.ibm.db2.jcc.DB2Driver class not found 

錯誤發生在這條線。

sessionFactory = cfg.buildSessionFactory(); 

部署到我的.war文件:
.WAR
| -WEB-INF
|| -lib
||| -db2jcc4.jar
||| -HIBERNATE,芯 - 4.1.2.Final.jar
||| -HIBERNATE-的EntityManager-4.1.2.Final.jar
||| -HIBERNATE-JPA-2.0-API-1.0.1.Final.jar

+0

你在哪裏得到錯誤,你可能會分享相關的堆棧跟蹤或其他形式的日誌? – aishwarya 2012-04-06 21:09:57

+0

我添加了堆棧跟蹤。它確實顯示配置文件正在被加載。 – SteveS 2012-04-09 13:55:03

回答

1

我不認爲我提到我正在使用JBoss7.1。最後,我無法弄清楚爲什麼Hibernate無法加載驅動程序,但通過切換到使用JNDI並在JBoss standalone.xml中配置數據源而不是嘗試從Hibernate建立連接來解決它。

0

我認爲這可能是因爲hibernate.cfg.xml錯誤。你應該檢查它是否在你的classpath中,或者你可以嘗試像這樣在你的方法調用中添加classpath。

SessionFactory sessionFactory = new Configuration().configure(
        "/com/mkyong/persistence/hibernate.cfg.xml") 
        .buildSessionFactory();

+0

感謝您的回覆。我確實嘗試移動我的配置文件,並在顯示的路徑中引用它。我遇到了同樣的問題。您可以在我的編輯中看到它正在查找配置文件。我已經確認db2jcc4.jar位於服務器上.war的lib文件夾中。另外,如前所述,Class.forName()會正確加載驅動程序。我也確認了網址是正確的。 – SteveS 2012-04-09 13:53:19

相關問題