简体   繁体   English

尝试使用休眠模式连接到mysql时连接被拒绝

[英]Connection refused when trying to connect to mysql using hibernate

I am trying to connect to mysql database using hibernate from the cfg file as 我正在尝试使用cfg文件中的休眠方式连接到mysql数据库

   <?xml version='1.0' encoding='utf-8'?>

 <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD//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.url">
            jdbc:mysql://localhost:3306/test</property>
        <property name="hibernate.connection.username">
            root</property>
        <property name="hibernate.connection.password">
            root</property>
        <property name="hibernate.connection.pool_size">
            10</property>
        <property name="show_sql">true</property>
        <property name="dialect">
            org.hibernate.dialect.MySQLDialect</property>
        <property name="hibernate.hbm2ddl.auto">
            update</property>

        <!-- Mapping files -->

        <mapping resource="Employee.hbm.xml" />
    </session-factory>
</hibernate-configuration>





   private static SessionFactory factory;

        public static void main(String[] args) {
            try {
                factory = new Configuration().configure().buildSessionFactory();
            } catch (Throwable ex) {
                System.err.println("Failed to create sessionFactory object." + ex);
                ex.fillInStackTrace();
                ex.printStackTrace();
                throw new ExceptionInInitializerError(ex);
            }

Though my connection-string,username,password are correct and the same are working fine in other project with basic jdbc connection, But when I try it using hibernate I am getting this error on console 虽然我的连接字符串,用户名,密码正确无误,并且在具有基本jdbc连接的其他项目中也能正常工作,但是当我使用休眠模式尝试运行时,在控制台上出现此错误

 Exception in thread "main" java.lang.ExceptionInInitializerError
    at com.hibernate.ManageEmployee.main(ManageEmployee.java:21)
Caused by: org.hibernate.HibernateException: Could not parse configuration: /hibernate.cfg.xml
    at com.hibernate.ManageEmployee.main(ManageEmployee.java:19)
Caused by: org.dom4j.DocumentException: Connection refused: connect Nested exception: Connection refused: connect
    at org.dom4j.io.SAXReader.read(SAXReader.java:484)
    at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1481)
    at org.hibernate.cfg.Configuration.configure(Configuration.java:1425)
    at org.hibernate.cfg.Configuration.configure(Configuration.java:1411)
    at com.hibernate.ManageEmployee.main(ManageEmployee.java:16)

And yes the database server is up and running but the connections from this hibernate application are refused 是的,数据库服务器已启动并正在运行,但是该休眠应用程序的连接被拒绝

The problem is that the xml parser is trying to access the schema file which is specified in the configuration file. 问题是xml解析器正在尝试访问配置文件中指定的架构文件。 Can the box, where you are running your program, access the internet? 您正在运行程序的盒子可以访问互联网吗?

Here is some additional information: 以下是一些其他信息:

https://forum.hibernate.org/viewtopic.php?f=1&t=949031 https://forum.hibernate.org/viewtopic.php?f=1&t=949031

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM