报错解决com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

当我用Hibernate连接MySql数据库并且简单的测试时,出现了

com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

这样一个问题

谷歌一下,出现的大多数解决方案都是让我修改my.ini的配置,修改wait_timeout,然而这个方法对于我来说并没有任何的用处。

于是我检查了一下我的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://hibernate</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.connection.password">123456</property>
        <!--配置hibernate的方言-->
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>

        <!--可选配置-->
        <!--打印sql语句-->
        <property name="hibernate.show_sql">true</property>
        <!--格式化sql-->
        <property name="hibernate.format_sql">true</property>
        <!--自动创建表-->
        <property name="hibernate.hbm2ddl.auto">update</property>

        <mapping resource="cn/fung/demo1/Customer.hbm.xml"></mapping>
    </session-factory>

确实,我不知道哪里出错了,这让我百思不得其解,于是我就检查了一下我的url

百度了一下url的标准写法:

驱动:com.mysql.jdbc.Driver 
URL:jdbc:mysql://machine_name:port/dbname 
注:machine_name:数据库所在的机器的名称; 
port:端口号,默认3306

按照标准形式改写之后,完美解决。

发布了25 篇原创文章 · 获赞 8 · 访问量 4万+

猜你喜欢

转载自blog.csdn.net/FungLi_notLove/article/details/88585760