Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Cannot insert record into Database!

 
v ray
Ranch Hand
Posts: 223
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,
Below is the hibernate program I am trying to execute:

contact.hbm.xml:


hibernate.cfg.xml:


FirstExample.java:


[ June 17, 2007: Message edited by: v ray ]
 
v ray
Ranch Hand
Posts: 223
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm getting the error:

org.hibernate.exception.JDBCConnectionException: Cannot open connection

Where have I gone wrong?? I'm checking the configuration file, it looks fine.
Next, I look at
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();

which looks fine too.
Is there something wrong with this line??

Transaction tx = session.beginTransaction();

Cant figure it out!!!
 
Jaikiran Pai
Marshal
Pie
Posts: 10447
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you post the entire exception stacktrace.

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


Also if i remember correctly, by default, the 'root' user in MySQL does not have a password. You could try using the following:


[ June 17, 2007: Message edited by: Jaikiran Pai ]
 
v ray
Ranch Hand
Posts: 223
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No I've set the password as "root" as well.
As for the StackTrace:

log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
Inserting Record .......
org.hibernate.exception.JDBCConnectionException: Cannot open connection
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:74)
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:420)
at org.hibernate.jdbc.ConnectionManager.getConnection(ConnectionManager.java:144)
at org.hibernate.jdbc.AbstractBatcher.prepareStatement(AbstractBatcher.java:93)
at org.hibernate.jdbc.AbstractBatcher.prepareStatement(AbstractBatcher.java:86)
at org.hibernate.jdbc.AbstractBatcher.prepareBatchStatement(AbstractBatcher.java:171)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2048)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2427)
at org.hibernate.action.EntityInsertAction.execute(EntityInsertAction.java:51)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:248)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:232)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:139)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:297)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:985)
at roseindia.tutorial.hibernate.FirstExample.main(FirstExample.java:28)
Caused by: java.sql.SQLException: Communication link failure: java.io.IOException, underlying cause: Unexpected end of input stream

** BEGIN NESTED EXCEPTION **

java.io.IOException
MESSAGE: Unexpected end of input stream

STACKTRACE:

java.io.IOException: Unexpected end of input stream
at com.mysql.jdbc.MysqlIO.readPacket(MysqlIO.java:339)
at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:782)
at com.mysql.jdbc.Connection.createNewIO(Connection.java:1585)
at com.mysql.jdbc.Connection.<init>(Connection.java:524)
at com.mysql.jdbc.Driver.connect(Driver.java:359)
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.ConnectionManager.openConnection(ConnectionManager.java:417)
at org.hibernate.jdbc.ConnectionManager.getConnection(ConnectionManager.java:144)
at org.hibernate.jdbc.AbstractBatcher.prepareStatement(AbstractBatcher.java:93)
at org.hibernate.jdbc.AbstractBatcher.prepareStatement(AbstractBatcher.java:86)
at org.hibernate.jdbc.AbstractBatcher.prepareBatchStatement(AbstractBatcher.java:171)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2048)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2427)
at org.hibernate.action.EntityInsertAction.execute(EntityInsertAction.java:51)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:248)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:232)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:139)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:297)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:985)
at roseindia.tutorial.hibernate.FirstExample.main(FirstExample.java:28)


** END NESTED EXCEPTION **


at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:876)
at com.mysql.jdbc.Connection.createNewIO(Connection.java:1585)
at com.mysql.jdbc.Connection.<init>(Connection.java:524)
at com.mysql.jdbc.Driver.connect(Driver.java:359)
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.ConnectionManager.openConnection(ConnectionManager.java:417)
... 14 more
 
Jaikiran Pai
Marshal
Pie
Posts: 10447
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Which version of MySQL JDBC driver jar do you have in your application classpath? Try using the latest version. A google search on this error led me to some posts which mentioned that this was because of using older version of MySQL jar.
 
v ray
Ranch Hand
Posts: 223
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have the 3.1 version, I think this is the latest one? Did someone try to execute this program on their system, maybe that will shed some light on what the mistake is?
The only part of the code I havent posted is the "Contact" class which has getter and setter methods for firstname,lastname, id and email.. Please help!
 
Jeff Hemminger
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I assume you can access your mysql with the same credentials from another jdbc client? I like to use SquirrelSQL, because I can configure it to use the same drivers as my application.

Anyway, just want to verify that the issue isn't a connection issue rather than a spring issue.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic