-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: Error in connecting to MySQL Data base
PostPosted: Mon Mar 10, 2008 10:40 am 
Newbie

Joined: Mon Mar 10, 2008 10:15 am
Posts: 1
Location: india
Hi,I am new to hibernate. i had written the following files.and i got the follwoing error.Please help me.

Hibernate version: 3.0


Mapping documents:
Contact.hbm.xml
Code:
<?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-mapping>
  <class name="com.prime.hibernate.Contact" table="TEST">
      <id name="testNo" type="int" column="TESTNO" >
         <generator class="assigned"/>
      </id>
      <property name="testName">
         <column name="TESTNAME" />
      </property>
   </class>
</hibernate-mapping>


hibernate.cfg.xml
Code:
   <?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:3306/test
      </property>
      <property name="hibernate.connection.username">root</property>
      <property name="hibernate.connection.password">root</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>


Code between sessionFactory.openSession() and session.close():

Code:
   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.setTestNo(104);
        contact.setTestName("Deepak");
        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();

Full stack trace of any exception that occurs:
Code:
log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
Inserting Record
Done
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.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
   at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:29)
   at org.hibernate.jdbc.AbstractBatcher.openConnection(AbstractBatcher.java:384)
   at org.hibernate.jdbc.JDBCContext.connect(JDBCContext.java:141)
   at org.hibernate.jdbc.JDBCContext.connection(JDBCContext.java:88)
   at org.hibernate.jdbc.AbstractBatcher.prepareStatement(AbstractBatcher.java:73)
   at org.hibernate.jdbc.AbstractBatcher.prepareStatement(AbstractBatcher.java:66)
   at org.hibernate.jdbc.AbstractBatcher.prepareBatchStatement(AbstractBatcher.java:130)
   at org.hibernate.persister.entity.BasicEntityPersister.insert(BasicEntityPersister.java:1809)
   at org.hibernate.persister.entity.BasicEntityPersister.insert(BasicEntityPersister.java:2171)
   at org.hibernate.action.EntityInsertAction.execute(EntityInsertAction.java:46)
   at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:239)
   at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:223)
   at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:136)
   at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:274)
   at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
   at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:669)
   at com.prime.hibernate.InsertData.main(InsertData.java:39)
Caused by: java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES)
   at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:946)
   at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2941)
   at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:868)
   at com.mysql.jdbc.MysqlIO.secureAuth411(MysqlIO.java:3340)
   at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1238)
   at com.mysql.jdbc.Connection.createNewIO(Connection.java:2743)
   at com.mysql.jdbc.Connection.<init>(Connection.java:1553)
   at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:266)
   at java.sql.DriverManager.getConnection(DriverManager.java:525)
   at java.sql.DriverManager.getConnection(DriverManager.java:140)
   at org.hibernate.connection.DriverManagerConnectionProvider.getConnection(DriverManagerConnectionProvider.java:110)
   at org.hibernate.jdbc.AbstractBatcher.openConnection(AbstractBatcher.java:381)
   ... 15 more


Name and version of the database you are using:
MySQL 5.0

Thanks in advance
--Suresh--

_________________
ch.suresh


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 10, 2008 10:53 am 
Newbie

Joined: Sun Jul 29, 2007 9:10 am
Posts: 8
Well. At first glance it appears that your user does not have the permission to access the designated schema. At least that was always the case when I got that kind of error message. As far as I remember the root user does not have any privileges for any schema. You have to assign it manually.

Hope that helps.
Martin


Top
 Profile  
 
 Post subject: by the way, a note on log4j
PostPosted: Mon Mar 10, 2008 3:32 pm 
Newbie

Joined: Mon Mar 10, 2008 2:48 pm
Posts: 2
I noticed that you have the following warnings

Quote:
log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.


I recommend you get log4j working to help with debugging.

With respect to the file which contains your main() function, add the following import statements:
Quote:
import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Logger;


And add the following 2 lines of code to your class declaration:

Quote:
static { BasicConfigurator.configure(); }
static Logger logger = Logger.getLogger(RoleTest.class);


You will need the following jar file:

Quote:
log4j-1.2.11.jar


(or some other version)

And as Martin said, I found that I could not connect as "root", so I created a user specific to the application and gave him rights specific to the application tables.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.