2017-04-24 80 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

相关问题