我正在使用 hibernate 将 Java 程序连接到 mysql 数据库,但出现错误

堆栈跟踪:


Exception in thread "main" org.hibernate.exception.JDBCConnectionException: Could not open connection

    at org.hibernate.exception.internal.SQLExceptionTypeDelegate.convert(SQLExceptionTypeDelegate.java:67)

    at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:49)

    at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:125)

    at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:110)

    at org.hibernate.engine.jdbc.internal.LogicalConnectionImpl.obtainConnection(LogicalConnectionImpl.java:304)

    at org.hibernate.engine.jdbc.internal.LogicalConnectionImpl.getConnection(LogicalConnectionImpl.java:169)

    at org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction.doBegin(JdbcTransaction.java:67)

    at org.hibernate.engine.transaction.spi.AbstractTransactionImpl.begin(AbstractTransactionImpl.java:160)

    at org.hibernate.internal.SessionImpl.beginTransaction(SessionImpl.java:1396)

    at com.telusko.DemoHibb.App.main(App.java:24)

我的 Hibernate.cfg.xml 文件是:


<?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/neon</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="hbm2ddl.auto">update</property>

    </session-factory>

</hibernate-configuration>


繁星coding
浏览 114回答 2
2回答

心有法竹

请检查您的 MySQL 服务器是否正在运行:对于 Unix&nbsp;(sh):sudo&nbsp;netstat&nbsp;-npl&nbsp;|&nbsp;grep&nbsp;3306对于 Windows(cmd):netstat&nbsp;-aon&nbsp;|&nbsp;find&nbsp;/i&nbsp;"3306"如果为空则运行 MySQL-server或更改端口

慕运维8079593

问题在于旧版本的 MySQL Connector/J 与 MySQL 8 的兼容性。我只需将 SQL Connector 版本升级为最新版本。经过一番努力,我能够找到问题所在。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java