Netbeans 7.1 Hibernate Reverse Engineering向导找不到数据库驱动程序

时间:2012-02-01 22:10:48

标签: java hibernate netbeans reverse-engineering

我目前正在尝试通过这个netbeans + hibernate + JavaSE教程(http://netbeans.org/kb/docs/java/hibernate-java-se.html)。一切都很好,但是在创建hibernate.cfg.xml之后,当涉及到应该应用逆向工程的部分时,反向工程向导会告诉我一些奇怪的消息:

"The database drivers are not added to the project classpath." 
"Go to project properties to add database library.". 

嗯,这有点奇怪,因为hibernate.cfg.xml是由netbeans生成的。我已经用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/sakila</property>
    <property name="hibernate.connection.username">root</property>
    <property name="hibernate.connection.password">apassword</property>
     <property name="hibernate.query.factory_class">org.hibernate.hql.classic.ClassicQueryTranslatorFactory</property>
  </session-factory>
</hibernate-configuration>

6 个答案:

答案 0 :(得分:2)

在hibernate.cfg.xml中添加以下代码行

<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/DATABASE_NAME</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password"/>
</session-factory>

答案 1 :(得分:1)

除了丢失jar文件之外,这是另一种可能导致netbeans中出现此错误的方法:配置文件错误。

因此,请确保您具有正确的配置文件(.cfg.xml文件)来构建.reveng

答案 2 :(得分:1)

附上示例配置:

<?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.connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="hibernate.connection.password">1234</property>
    <property name="hibernate.connection.url">jdbc:mysql://161.58.103.144:3306/exampleDatabase?zeroDateTimeBehavior=convertToNull</property>
    <property name="hibernate.connection.username">JasonGlez</property>
    <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
  </session-factory>
</hibernate-configuration>

只需更改IP,数据库名称,用户名和密码

即可

答案 3 :(得分:0)

您缺少JDBC驱动程序。在项目视图中右键单击项目下的Libraries节点,然后选择Add Library... -> MySQL JDBC Driver

答案 4 :(得分:0)

如你所说,请确保hibernate.cfg.xml是好的。尝试从Netbeans Hibernate配置向导重新生成它,而不是自己制作它。还尝试重新创建项目并确保新项目向导使用数据库连接设置填充hibernate.cfg.xml。它第一次出现在我身上;不知道为什么。在它开始工作之前,我还重新启动了Netbeans和计算机来启动,这可能不会受到影响。 (没有双关语。)

答案 5 :(得分:0)

这需要MySQL驱动程序,在库中添加我的sql驱动程序来解决问题