简体   繁体   中英

Hibernate jdbc no suitable driver found

I'm receiving the dreaded No suitable driver found error when using Hibernate. I'm pretty sure the driver jar is in the classpath as I can do Class.forName("com.mysql.jdbc.Driver") . In fact for now I just call this before building the session factory and my code works. But is this 'safe'? Why is this happening? Here is my config:

<session-factory>
    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/test</property>

    <property name="hibernate.connection.username">root</property>
    <property name="hibernate.connection.password">password</property>

    <property name="connection.pool_size">1</property>

    <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>

    <property name="show_sql">true</property>
    <property name="format_sql">true</property>
    <property name="use_sql_comments">false</property>

    <mapping resource="User.hbm.xml" />
</session-factory>

Thank you

It sounds like the JAR is in the CLASSPATH . You'd get a NoClassDefFound exception if that wasn't the case.

No suitable driver usually means that the connection URL syntax isn't correct for the given driver.

MySQL needs "jdbc:mysql://server:3306/database" - yours looks correct.

If it can't resolve localhost, you'll have a problem. Maybe 127.0.0.1 will work better.

Is MySQL listening on port 3306?

Does the database you asked to connected to exist? Can you log in via the MySQL client and see tables and data?

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