Hibernate链接数据库出现未见过的错误

一直使用SSH框架都很正常,前几天朋友让我当他面做个Hibernate的例子,当场就报了这个奇怪的错误。
然后再MyEclipse中调用之前写的Hibernate例子,也无法运行。
在MyEclipse的package explorer中,有好多以前写的例子,都是能顺利运行的,现在情况是SSH框架,JDBC的都可以单独运行,唯独纯Hibernate写的无法运行。
程序如下:
package com.test;
import java.util.Date;
public class User {
private int id;
private String name;
private Date birthday;

public int getId() {
    return id;
}
public void setId(int id) {
    this.id = id;
}
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
public Date getBirthday() {
    return birthday;
}
public void setBirthday(Date birthday) {
    this.birthday = birthday;
}

}

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">








hibernate.cfg.xml
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">


com.mysql.jdbc.Driver
jdbc:mysql://localhost:3060/hibernatetrain
root
123456
org.hibernate.dialect.MySQLDialect
create


测试程序:
package com.test;

import java.util.Date;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

public class TestUser {
public static void main(String [] args){
Configuration cfg = new Configuration();
cfg.configure();

    SessionFactory sf = cfg.buildSessionFactory();
    Session s = sf.openSession();

    Transaction tx = s.beginTransaction();
    User user = new User();
    user.setName("Hoffman");
    user.setBirthday(new Date());
    s.save(user);
    tx.commit();
    s.close();
}

}

以上数据库链接,用户名和密码都是多次检查的,没有问题。
在Transaction tx = s.beginTransaction();这步,出现了以下错误:

log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
Exception in thread "main" org.hibernate.exception.JDBCConnectionException: Cannot open connection
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:74)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:29)
at org.hibernate.jdbc.ConnectionManager.openConnection(ConnectionManager.java:420)
at org.hibernate.jdbc.ConnectionManager.getConnection(ConnectionManager.java:144)
at org.hibernate.jdbc.JDBCContext.connection(JDBCContext.java:129)
at org.hibernate.transaction.JDBCTransaction.begin(JDBCTransaction.java:57)
at org.hibernate.impl.SessionImpl.beginTransaction(SessionImpl.java:1290)
at com.test.TestUser.main(TestUser.java:19)
Caused by: com.mysql.jdbc.CommunicationsException: Communications link failure due to underlying exception:

** BEGIN NESTED EXCEPTION **

java.net.SocketException
MESSAGE: java.net.ConnectException: Connection refused: connect

STACKTRACE:

java.net.SocketException: java.net.ConnectException: Connection refused: connect
at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:156)
at com.mysql.jdbc.MysqlIO.(MysqlIO.java:276)
at com.mysql.jdbc.Connection.createNewIO(Connection.java:2666)
at com.mysql.jdbc.Connection.(Connection.java:1531)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:266)
at java.sql.DriverManager.getConnection(DriverManager.java:582)
at java.sql.DriverManager.getConnection(DriverManager.java:154)
at org.hibernate.connection.DriverManagerConnectionProvider.getConnection(DriverManagerConnectionProvider.java:110)
at org.hibernate.jdbc.ConnectionManager.openConnection(ConnectionManager.java:417)
at org.hibernate.jdbc.ConnectionManager.getConnection(ConnectionManager.java:144)
at org.hibernate.jdbc.JDBCContext.connection(JDBCContext.java:129)
at org.hibernate.transaction.JDBCTransaction.begin(JDBCTransaction.java:57)
at org.hibernate.impl.SessionImpl.beginTransaction(SessionImpl.java:1290)
at com.test.TestUser.main(TestUser.java:19)

** END NESTED EXCEPTION **

Last packet sent to the server was 0 ms ago.
at com.mysql.jdbc.Connection.createNewIO(Connection.java:2741)
at com.mysql.jdbc.Connection.(Connection.java:1531)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:266)
at java.sql.DriverManager.getConnection(DriverManager.java:582)
at java.sql.DriverManager.getConnection(DriverManager.java:154)
at org.hibernate.connection.DriverManagerConnectionProvider.getConnection(DriverManagerConnectionProvider.java:110)
at org.hibernate.jdbc.ConnectionManager.openConnection(ConnectionManager.java:417)
... 5 more

以上的例子,新建工程,连同样的数据库,手动建表,用JDBC就能插入数据。后来我又建立新的工程,用Spring来管理Hibernate的SessionFactory链接相同的数据库,依旧正常。唯独这个纯Hibernate工程,始终抱着个错误!
我用的是MyEclipse 8,自导入Hibernate 3.2的jar包。

后来我取消了Hibernate的自动建表配置,手动建了表,还是出现同样的错误。以前出现Connection refused: connect错误,我检查数据库链接就OK,但是这次能查的地方都查了,数据库服务也是启动的,就是找不出任何原因导致这样的错误。
我怀疑是MySQL数据库,但是把这个例子链接同事机器的库依然报出相同的错误。

各位朋友,很抱歉我没有积分了,无法给大家加分,但还是希望有人能帮助我解决这个问题,抱歉。

4个回答

虽说你一再强调端口正常,可还是怀疑你的端口错误,因为mysql默认的端口是3306,而你上面提供的端口是3060,会不会是笔误呢,除非你装mysql时改变了默认的端口?

是不是你端口配置错误?从异常信息来看比较可能是这个问题。

mysql驱动包一致吗?

还有一点就是你的po类com.test.user没有实现java.io.Serializable序列化接口,不清楚和这个有没有关系,不妨试下

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问