【问题标题】:Eclipse - Hibernate : No suitable driver found for jdbc:mysql://localhost:3306/hibernatedbEclipse - Hibernate:找不到适合 jdbc:mysql://localhost:3306/hibernatedb 的驱动程序
【发布时间】:2017-09-20 17:35:00
【问题描述】:

我正在尝试通过编写一个简单的 java 程序来学习 Hibernate。我使用 MySQL 作为数据库,运行程序时出现上述错误。我在互联网上看到了许多解决方案,并尝试了所有可能的方法,但无济于事。我做错了什么?

配置文件:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
    <session-factory>
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernatedb</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.connection.password">root</property>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="show_sql">true</property>
        <property name="format_sql">true</property>
        <property name="hbm2ddl.auto">create</property>
        <mapping resource="com/test/hibernate/student.hbm.xml" />
    </session-factory>
</hibernate-configuration>

控制台输出:

log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
Exception in thread "main" org.hibernate.exception.JDBCConnectionException: Cannot open connection
    at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:74)
    at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
    at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:29)
    at org.hibernate.jdbc.ConnectionManager.openConnection(ConnectionManager.java:420)
    at org.hibernate.jdbc.ConnectionManager.getConnection(ConnectionManager.java:144)
    at org.hibernate.jdbc.JDBCContext.connection(JDBCContext.java:119)
    at org.hibernate.transaction.JDBCTransaction.begin(JDBCTransaction.java:57)
    at org.hibernate.impl.SessionImpl.beginTransaction(SessionImpl.java:1326)
    at com.test.hibernate.SimpleTest.main(SimpleTest.java:23)
Caused by: java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/hibernatedb
    at java.sql.DriverManager.getConnection(DriverManager.java:689)
    at java.sql.DriverManager.getConnection(DriverManager.java:208)
    at org.hibernate.connection.DriverManagerConnectionProvider.getConnection(DriverManagerConnectionProvider.java:110)
    at org.hibernate.jdbc.ConnectionManager.openConnection(ConnectionManager.java:417)
    ... 5 more

类路径和 lib 文件夹:

【问题讨论】:

  • 在您的 CLASSPATH 中添加 MySql Connector/J 驱动程序以解决问题。
  • @N00bPr0grammer ,看起来它已经包含在 OP 提供的图像中
  • 尝试使用 jdk 代替 jre
  • 您是从 Eclipse 还是从命令行运行程序?
  • @gundev 刚试过,不行。

标签: java mysql eclipse hibernate jdbc


【解决方案1】:

很可能您没有正确添加 mysql-connector 库,因为在您发布的运行配置的快照中,该库出现在一个名为“hibernate test”的项目中,其中包含一个名为“mysql-connector-etc”的文件.jar”,但这不是在类路径中设置 JAR 的方法。

这样做:最好在项目的 Java 构建路径中添加“mysql-connector-etc.jar”:弹出项目的上下文菜单和Build Path &gt; Configure Build Path &gt; Libraries &gt; Add external jars。然后选择 mysql-connector JAR 并输入。从那时起,Eclipse 将把这个 JAR 包含在您应该执行的项目的任何执行中(这样您就不必再关心它了)。

【讨论】:

    猜你喜欢
    • 2019-08-14
    • 2016-10-01
    • 2012-05-08
    • 2014-07-28
    • 1970-01-01
    • 1970-01-01
    • 2012-05-27
    • 2013-05-09
    • 2012-07-30
    相关资源
    最近更新 更多