Hibernate - MySQL - 连接到数据库

问题描述 投票:0回答:2

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

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但我仍然不明白出了什么问题。

致以诚挚的问候。
安德里亚

java mysql hibernate connection driver
2个回答
0
投票

我从未使用过 Hibernate 映射向导,但据我所知,这对于创建带有扩展名的 Hibernate 映射文件很有用

.hbm.xml
:请参阅 此链接 作为参考。

因此结果是一个 .hbm.xml 文件,它是类到表的映射。

重点是在您的

hibernate.cfg.xml
中没有明确引用此 .hbm.xml 文件。

尝试添加:

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

在您的

hibernate.cfg.xml
中,并确保提供使用 Hibernate 映射向导工具时所需的所有数据。

希望这会有用!

再见!


0
投票

南巴里和保罗,

我必须将 config.prop 和 mysql 驱动程序的“Path”库放入 CLASSPATH 变量中。现在可以了。

谢谢大家!

再见保罗,谢谢!

© www.soinside.com 2019 - 2024. All rights reserved.