2017-04-24 86 views
1

我想通過編寫一個簡單的java程序來學習Hibernate。我使用MySQL作爲數據庫,運行程序時出現上述錯誤。我在互聯網上看到了很多這樣的解決方案,並嘗試盡一切可能無濟於事。我究竟做錯了什麼?Eclipse - Hibernate:找不到適合jdbc的驅動程序:mysql:// localhost:3306/hibernatedb

配置文件:

<?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> 
     <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> 
     <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernatedb</property> 
     <property name="hibernate.connection.username">root</property> 
     <property name="hibernate.connection.password">root</property> 
     <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> 
     <property name="show_sql">true</property> 
     <property name="format_sql">true</property> 
     <property name="hbm2ddl.auto">create</property> 
     <mapping resource="com/test/hibernate/student.hbm.xml" /> 
    </session-factory> 
</hibernate-configuration> 

控制檯輸出:

log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment). 
log4j:WARN Please initialize the log4j system properly. 
Exception in thread "main" org.hibernate.exception.JDBCConnectionException: Cannot open connection 
    at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:74) 
    at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43) 
    at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:29) 
    at org.hibernate.jdbc.ConnectionManager.openConnection(ConnectionManager.java:420) 
    at org.hibernate.jdbc.ConnectionManager.getConnection(ConnectionManager.java:144) 
    at org.hibernate.jdbc.JDBCContext.connection(JDBCContext.java:119) 
    at org.hibernate.transaction.JDBCTransaction.begin(JDBCTransaction.java:57) 
    at org.hibernate.impl.SessionImpl.beginTransaction(SessionImpl.java:1326) 
    at com.test.hibernate.SimpleTest.main(SimpleTest.java:23) 
Caused by: java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/hibernatedb 
    at java.sql.DriverManager.getConnection(DriverManager.java:689) 
    at java.sql.DriverManager.getConnection(DriverManager.java:208) 
    at org.hibernate.connection.DriverManagerConnectionProvider.getConnection(DriverManagerConnectionProvider.java:110) 
    at org.hibernate.jdbc.ConnectionManager.openConnection(ConnectionManager.java:417) 
    ... 5 more 

的Classpath & lib文件夾: Classpath & lib folder

lib folder

+0

在您的CLASSPATH上添加MySql Connector/J驅動程序來解決問題。 –

+0

@ N00bPr0grammer,看起來像它已經包含在其中的圖像由OP –

+0

提供嘗試使用jdk而不是jre –

回答

0

最可能的是你沒有請正確添加mysql連接器庫,因爲在發佈的Run配置的快照中,該庫出現在名爲「hibernate test」的項目中,該項目包含名爲「mysql-connector-etc.jar」的文件,但那不是在類路徑中設置JAR的方法。

這樣做:最好在項目的Java Build Path中添加「mysql-connector-etc.jar」:彈出項目的上下文菜單,並彈出Build Path > Configure Build Path > Libraries > Add external jars。然後選擇mysql連接器JAR並輸入。從那以後,Eclipse將把這個JAR包含到你應該執行的任何項目執行中(這樣你就不必再關心它了)。

+0

謝謝。已經做到了.. – Ruwangi

相關問題