1
Javaのhibernateをmysqlで使用する方法を学習しようとしていますが、私がダウンロードしたサンプルを実行する際に問題が発生しています。次のコードが含まれていますJava SQLの「構文エラーまたはアクセス違反」
<?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/hibernatetutorial</property>
<property name="hibernate.connection.username">web</property>
<property name="hibernate.connection.password">web</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="contact.hbm.xml"/>
</session-factory>
</hibernate-configuration>
と(http://www.roseindia.net/hibernate/runninge-xample.shtmlからダウンロードした):主な機能:
はException in thread "main" org.hibernate.exception.GenericJDBCException: Cannot open connection
at org.hibernate.exception.ErrorCodeConverter.handledNonSpecificException(ErrorCodeConverter.java:92)
at org.hibernate.exception.ErrorCodeConverter.convert(ErrorCodeConverter.java:80)
....
at org.hibernate.persister.entity.BasicEntityPersister.insert...
**Caused by: java.sql.SQLException: Syntax error or access violation message from server: "Access denied for user 'web'@'localhost' to database 'hibernatetutorial'"**
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:1997)
...
at org.hibernate.connection.DriverManagerConnectionProvider.getConnectio...
私の設定ファイルは、次のコードが含まれています 私は次のエラーを取得しています
public static void main(String[] args) {
Session session = null;
try{
// This step will read hibernate.cfg.xml and prepare hibernate for use
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
session =sessionFactory.openSession();
//Create new instance of Contact and set values in it by reading them from form object
System.out.println("Inserting Record");
Contact contact = new Contact();
contact.setId(6);
contact.setFirstName("Deepak");
contact.setLastName("Kumar");
contact.setEmail("[email protected]");
session.save(contact);
System.out.println("Done");
}catch(Exception e){
System.out.println(e.getMessage());
}finally{
// Actual contact insertion will happen at this step
session.flush();
session.close();
}
私はすべての必要な権限を持つ新しい "web"ユーザーを作成しました:
CREATE USER 'web'@'localhost' IDENTIFIED BY 'web';
GRANT ALL PRIVILEGES ON hibernatetutorial.* TO [email protected] IDENTIFIED BY 'web';
私は "hibernatetutorial"データベースを作成しました。 私が間違っていることは何ですか?私はこれをデバッグする方法も知らない。
おかげで、 李
正しいかも。それを変更した後もエラーは発生します(私は "root"ユーザーで最初に試してみたのと同じエラーが発生していますが、ユーザーを "web"に変更してアクセス権の問題ではないことを確かめました。 ()。 – user429400