简体   繁体   English

休眠 - MySQL - 连接到数据库

[英]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:我正在尝试使用带有 NetBeans 7.4 的 Hibernate 3.6.10 连接到 MySQL 数据库,但是当我尝试创建新的“Hibernate Mapping Wizard”时,我回复了以下错误消息:

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:这是我的“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>

I've found another similar problem here: Hibernate - Cannot connect to DB but I still not understand what's wrong.我在这里发现了另一个类似的问题: Hibernate - Cannot connect to DB但我仍然不明白出了什么问题。

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.我从未使用过休眠映射向导,但据我所知,这对于创建扩展名为.hbm.xml的休眠映射文件很有用:请参阅此链接作为参考。

So the result is a .hbm.xml file that is the mapping of your class to your table.所以结果是一个 .hbm.xml 文件,它是你的类到你的表的映射。

The point is the in your hibernate.cfg.xml there is no explicit reference to this .hbm.xml file.关键是在您的hibernate.cfg.xml中没有明确引用此 .hbm.xml 文件。

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.在您的hibernate.cfg.xml中,并确保提供使用 Hibernate 映射向导工具时所需的所有数据。

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.我不得不将我的 config.prop 和 mysql 驱动程序的“路径”库放入 CLASSPATH 变量中。 Now it works.现在它起作用了。

Thankyou all!谢谢你们!

Ciao Paolo, grazie! Ciao Paolo,天哪!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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