【发布时间】:2014-03-25 12:24:33
【问题描述】:
我正在尝试使用 cfg 文件中的 hibernate 连接到 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 试图在你的堆栈跟踪中连接(只是配置)。我的猜测是你的 XML 解析器试图连接到某个地方以获取 DTD/XSD 进行验证。你能提供完整的堆栈跟踪?还要检查这个stackoverflow.com/questions/17114046/…
-
@udaykiranpulipati : 我已经创建了密码
-
@rkosegi :检查我添加了完整堆栈跟踪的编辑
-
@Rishabh 您放置 hibernate.cfg.xml 的位置。如果它不在 src 根文件夹中,请将其放在那里。