初学hibernate , org.hibernate.exception.GenericJDBCException: Cannot open connection 大家帮帮忙!!鞠躬了!!!
初学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