2011-12-14 50 views
0

当我与我的Linux(Ubuntu 11.10)与MySQL版本5.1上的代码连接它工作正常,但是当我想在Windows操作系统启动我的应用程序(Windows 7的MySQL 5.5)它抛出异常:Hibernate,JDBC,Mysql,无法登录到数据库

org.hibernate.exception.GenericJDBCException: Cannot open connection 

在这两种情况下我都在本地主机上登录。 我试图登录到root用户。我完全确定这是正确的密码。 我试图盛大所有priviligates新的用户,但它是同样的例外。 另一种选择,我是尝试是,我赶上GenericJDBCException和节目给了我这样的:

java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES) 

我连接到数据库编程(通过休眠AnnotationConfiguration):

annotationConfiguration = new AnnotationConfiguration(); 
annotationConfiguration.setProperty("hibernate.hbm2ddl.auto", "create"); 

// add annotatedClasses 
annotationConfiguration.addAnnotatedClass(X.class); 

annotationConfiguration.setProperty("hibernate.dialect", "net.sf.hibernate.dialect.MySQLDialect"); 
annotationConfiguration.setProperty("hibernate.connection.driver_class", "com.mysql.jdbc.Driver"); 
annotationConfiguration.setProperty("hibernate.connection.url", "jdbc:mysql://" + host + ":3306/" + database); 
annotationConfiguration.setProperty("hibernate.connection.username", userName); 
annotationConfiguration.setProperty("hibernate.connection.password", password); 
annotationConfiguration.setProperty("hibernate.connection.release_mode", "auto"); 
//annotationConfiguration.setProperty("hibernate.query.factory_class", "org.hibernate.hql.classic.ClassicQueryTranslatorFactory"); 
if (StaticConstants.DEBUG) { 
    annotationConfiguration.setProperty("hibernate.show_sql", "true"); 
} else { 
    annotationConfiguration.setProperty("hibernate.show_sql", "false"); 
} 
annotationConfiguration.configure(); 
sessionFactory = annotationConfiguration.buildSessionFactory(); 
sessionFactory.close(); 

我使用Hibernate 3.6.7版,但我通过配置类在Hibernate 3.6.8上测试了它,结果相同。

所以我的问题是有人有同样的问题和解决它,或任何人有任何想法解决它?

谢谢

+0

你能连接到数据库使用命令行?即mysql -u root -p – 2011-12-14 09:51:44

+0

是的,我可以做到。 – 2011-12-14 09:52:57

回答

1

首先尝试从命令提示符登录到安装在win 7上的mysql。 打开命令promt.Move到mysql安装的bin文件夹。

C:\ mysql的\ BIN> -u的mysql.exe根-p

上面的行会提示输入密码password.Input,看看你是否能正确登录