2013-10-28 114 views
2

我试图用NetBeans 7.4连接到使用Hibernate 3.6.10的MySQL数据库,但是当我尝试创建新的“Hibernate映射向导”时,我回复此错误消息:Hibernate - MySQL - 连接数据库

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

这是我的“的hibernate.cfg.xml”文件:

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

我已经在这里发现了另一个类似的问题:Hibernate - Cannot connect to DB但我还是不明白什么是错的。

此致敬礼。
安德烈

+0

类路径中的com.mysql.jdbc.Driver'类是什么? –

+0

你在classpath中是否也有hibernate config.prop文件? – kosa

+0

嗨罗宾和Nambari:我不得不把我的mysql驱动程序的“路径”库也config.prop路径CLASSPATH变量,但我没有这样做。谢谢。 – user2928795

回答

0

我从来没有使用Hibernate映射向导,但据我所知,这是创建一个Hibernate映射文件的扩展名.hbm.xml有用:看this link作为参考。

所以结果是一个.hbm.xml文件,它是你的类到你的表的映射。

重点是在你的hibernate.cfg.xml没有明确提到这个.hbm.xml文件。

尝试添加:

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

hibernate.cfg.xml和一定要给使用Hibernate映射向导工具时所需的所有数据。

希望这会有用!

Ciao!

0

Nambari和保罗,

我不得不把我的config.prop的和MySQL驱动的“路径”库到CLASSPATH变量。现在它可以工作。

谢谢你!

Ciao Paolo,grazie!