简体   繁体   中英

Hibernate - MySQL - Connection to DB

I'm trying to connect to a MySQL database using Hibernate 3.6.10 with NetBeans 7.4, but when I try to create a new "Hibernate Mapping Wizard" I reply this error message:

Unable to connect: Cannot establish a connection jdbc:mysql://localhost:3306/mydbname using apache.org.derby.jdbc.ClientDriver(Unable to find a suitable driver)

This is my "hibernate.cfg.xml" file:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
  <session-factory>
    <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/database</property>
    <property name="hibernate.connection.username">username</property>
    <property name="hibernate.connection.password">password</property>
  </session-factory>
</hibernate-configuration>

I've found another similar problem here: Hibernate - Cannot connect to DB but I still not understand what's wrong.

Best regards.
Andrea

I have never used Hibernate Mapping Wizard but as far as I know this is useful to create a hibernate mapping file with an extension .hbm.xml : see this link as reference.

So the result is a .hbm.xml file that is the mapping of your class to your table.

The point is the in your hibernate.cfg.xml there is no explicit reference to this .hbm.xml file.

Try to add:

<mapping resource="<your_generated_file>.hbm.xml"/>

in your hibernate.cfg.xml and be sure to give all the data needed when using the Hibernate Mapping Wizard tool.

Hope this will be useful!

Ciao!

Nambari and Paolo,

I had to put my config.prop's and mysql driver's "Path" library into CLASSPATH variable. Now it works.

Thankyou all!

Ciao Paolo, grazie!

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