简体   繁体   中英

Eclipse - Hibernate : No suitable driver found for jdbc:mysql://localhost:3306/hibernatedb

I am trying to learn Hibernate by writing a simple java program. I'm using MySQL as the database, and I get the above error when running the program. I saw a number of solutions for this on the internet and tried everything possible, to no avail. What am I doing wrong?

Configuration file:

<?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>

Console output:

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 folder: Classpath&lib文件夹

lib文件夹

Most probably you didn't add the mysql-connector library correctly, because in the snapshot from the Run configuration you posted, that library appears within a project called "hibernate test" which contains a file named "mysql-connector-etc.jar", but that is not the way to set a JAR within a classpath.

Do it this way: It's better that you add the "mysql-connector-etc.jar" right within your project's Java Build Path: Pop up the project's contextual menu, and the Build Path > Configure Build Path > Libraries > Add external jars . Then select the mysql-connector JAR and enter. From then on, Eclipse will include this JAR into any execution of your project you should do (so you won't have to care about it anymore).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM