2014-03-25 316 views
0

我试图使用从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); 
      } 

虽然我的连接字符串,用户名,密码是否正确,同样是工作与基本JDBC连接其他项目很好,但是当我尝试使用Hibernate我在控制台上

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) 

收到此错误它是的数据库服务器启动并运行,但是从这个Hibernate应用程序的连接将被拒绝

+0

你创建了测试数据库的密码。如果没有为测试数据库创建密码 – pudaykiran

+1

没有提到hibernate正在尝试连接你的堆栈跟踪(只是配置)。我猜想你的XML解析器试图连接某处以获取DTD/XSD进行验证。可以提供全栈跟踪?另外检查这一个http://stackoverflow.com/questions/17114046/hibernate-error-possibly-with-dtd-declaration – rkosegi

+0

@udaykiranpulipati:我已经创建了密码 – Rishabh

回答

1

问题是xml解析器试图访问配置文件中指定的模式文件。您可以在哪里运行程序,访问互联网?

下面是一些额外的信息:

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

+3

Hibernate可以在本地解析DTD(没有网络连接)。例如在这里http://stackoverflow.com/questions/4301294/cant-parse-hibernate-cfg-xml-while-offline – andih

+0

@andih:不得不下载dtd的本地副本...它的工作 – Rishabh