初学hibernate , org.hibernate.exception.GenericJDBCException: Cannot open connection 大家帮帮忙!!鞠躬了!!!

ascend_11 2007-11-25 02:22:49
初学hibernate   ~~~  大家帮帮忙!!鞠躬了!!!
软件环境:
JDK 1.6
tomcat 5.0.28
mysql 5.5
hibernate 3.2
文件:
3.
User.java:

package ch03.hibernate;

public class User {
private int id;
private String username;
private String password;
private String email;

/**
* @return Returns the email.
*/
public String getEmail() {
return email;
}
/**
* @param email The email to set.
*/
public void setEmail(String email) {
this.email = email;
}
/**
* @return Returns the id.
*/
public int getId() {
return id;
}
/**
* @param id The id to set.
*/
public void setId(int id) {
this.id = id;
}
/**
* @return Returns the password.
*/
public String getPassword() {
return password;
}
/**
* @param password The password to set.
*/
public void setPassword(String password) {
this.password = password;
}
/**
* @return Returns the username.
*/
public String getUsername() {
return username;
}
/**
* @param username The username to set.
*/
public void setUsername(String username) {
this.username = username;
}
}


2.
User.hbm.xml

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

<hibernate-mapping>

<class name="ch03.hibernate.User" table="Myusertable">
<id name="id">
<generator class="increment"/>
</id>
<property name="username"/>
<property name="password"/>
<property name="email"/>

</class>

</hibernate-mapping>

3.hibernate配置文件: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">

<hibernate-configuration>

<session-factory>

<!-- Database connection settings -->
<property

name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property

name="connection.url">jdbc:mysql://localhost:3306/MyProject</property

>
<property name="connection.username">root</property>
<property name="connection.password">root</property>

<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>

<!-- SQL dialect -->
<property

name="dialect">org.hibernate.dialect.MySQLDialect</property>

<!-- Enable Hibernate's automatic session context management

-->
<property

name="current_session_context_class">thread</property>

<!-- Disable the second-level cache -->
<property

name="cache.provider_class">org.hibernate.cache.NoCacheProvider</prop

erty>

<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>

<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">create</property>

<mapping resource="ch03/hibernate/User.hbm.xml"/>.hn

</session-factory>

</hibernate-configuration>

4. Test.java(测试类)
package ch03.hibernate;

import org.hibernate.*;
import org.hibernate.cfg.*;
public static void main(String[] args) {
try {
SessionFactory sf = new Configuration

().configure().buildSessionFactory();
Session session = sf.openSession();
Transaction tx = session.beginTransaction();
User user = new User();
user.setUsername("Hibernate");
user.setPassword("Fra22");
session.save(user);
session.save(user);
tx.commit();
session.close();
} catch (HibernateException e) {
e.printStackTrace();
}
}
}



错误提示信息:

log4j:WARN No appenders could be found for logger

(org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
org.hibernate.exception.GenericJDBCException: Cannot open connection
at

org.hibernate.exception.SQLStateConverter.handledNonSpecificException

(SQLStateConverter.java:103)
at org.hibernate.exception.SQLStateConverter.convert

(SQLStateConverter.java:91)
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:426)
at org.hibernate.jdbc.ConnectionManager.getConnection

(ConnectionManager.java:144)
at org.hibernate.jdbc.JDBCContext.connection

(JDBCContext.java:119)
at org.hibernate.transaction.JDBCTransaction.begin

(JDBCTransaction.java:57)
at org.hibernate.impl.SessionImpl.beginTransaction

(SessionImpl.java:1326)
at ch03.hibernate.Test.main(Test.java:22)
Caused by: java.sql.SQLException: Access denied for user

'root'@'localhost' (using password: YES)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2975)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:798)
at com.mysql.jdbc.MysqlIO.secureAuth411(MysqlIO.java:3700)
at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1203)
at com.mysql.jdbc.Connection.createNewIO

(Connection.java:2572)
at com.mysql.jdbc.Connection.<init>(Connection.java:1485)
at com.mysql.jdbc.NonRegisteringDriver.connect

(NonRegisteringDriver.java:266)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at

org.hibernate.connection.DriverManagerConnectionProvider.getConnectio

n(DriverManagerConnectionProvider.java:110)
at org.hibernate.jdbc.ConnectionManager.openConnection

(ConnectionManager.java:423)
... 5 more
...全文
9523 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
MOONNIFE 2011-10-12
  • 打赏
  • 举报
回复
给你说个,把你的数据库的编码改一下,改成GBK的 这个问题应该就没了 哈哈
a506690 2010-10-15
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 fenghunwj 的回复:]
我也出现了类似的问题 不过我是写SSH框架的程序时候出现的 数据库密码和名称没有问题
数据库也一直开着 但这个问题就会时不时的出现
该怎么解决啊???
求高人指点!!!
[/Quote]


我也遇到了类似问题,有没有解决的方法?我也是用的连接池
psyco 2010-09-27
  • 打赏
  • 举报
回复
我用连接池 刷新几次后就出现类似问题?
是不是数据库连接池的问题?
wanghai234 2010-07-09
  • 打赏
  • 举报
回复
org.hibernate.LockMode[ ]、org.hibernate.type.EntityType[ ]、org.hibernate.loader.EntityAliases[]、org.hibernate.persister.entity.Loadable[ ]、org.hibernate.type.Type[ ]、org.hibernate.property.Setter[ ]、org.hibernate.property.Getter[ ] 是什么意思啊
tinguo002 2009-11-28
  • 打赏
  • 举报
回复
我也是这上面的错误,经检查是我的hibernate配置文件里的 帐号和密码写错了
hww2008 2009-03-03
  • 打赏
  • 举报
回复
你的数据库服务没有启动
kingwyj 2008-09-04
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 fenghunwj 的回复:]
我也出现了类似的问题 不过我是写SSH框架的程序时候出现的 数据库密码和名称没有问题
数据库也一直开着 但这个问题就会时不时的出现
该怎么解决啊???
求高人指点!!!
[/Quote]

在连接池设置时,把maxActive值设大点试试。
fenghunwj 2008-09-02
  • 打赏
  • 举报
回复
我也出现了类似的问题 不过我是写SSH框架的程序时候出现的 数据库密码和名称没有问题
数据库也一直开着 但这个问题就会时不时的出现
该怎么解决啊???
求高人指点!!!
ascend_11 2007-12-02
  • 打赏
  • 举报
回复
谢谢各位,问题解决了,如大家所料,是密码错误的问题引发数据库联接不上问题
再次恭谢!
dangdang80 2007-11-26
  • 打赏
  • 举报
回复
数据库链接的问题,重新启动数据库,导入驱动
xiaocainiao0703 2007-11-25
  • 打赏
  • 举报
回复
貌似你的驱动包没有导入吧?????
狼烟的烟 2007-11-25
  • 打赏
  • 举报
回复
数据库链接失败,jdbc没有配置好吧。
好好看看你的xml配置。
ascend_11 2007-11-25
  • 打赏
  • 举报
回复
启动了,还是错误依旧!
sasgsc 2007-11-25
  • 打赏
  • 举报
回复
你MYSQL的服务启动了没?....
cd_tiger 2007-11-25
  • 打赏
  • 举报
回复
1、你没有配置log4j(配不配无所谓)
2、你的数据库的连接失败(这是根本)

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧