• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Cannot Open Connection problem in Hibernate + MySQL + Tomcat

 
Saravanamuthu Krishnan
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
I am a beginner to hibernate. I have prefered to use hibernate with tomcat and mysql.

I have done all things that are all to integrate hibernate and mysql. But i got an error.

org.hibernate.exception.GenericJDBCException: Cannot open connection

This is the configuration code in hibernate.cfg.xml

<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernatetutorial</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">syncro</property>
<property name="hibernate.connection.pool_size">10</property>
<property name="show_sql">true</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>

<mapping resource="contact.hbm.xml"/>
</session-factory>
</hibernate-configuration>

This is the mapping file

<hibernate-mapping>
<class name="com.test.hibernate.Contact" table="contact">
<id name="id" type="long" column="id">
<generator class="assigned"/>
</id>

<property name="firstName">
<column name="firstname"/>
</property>
<property name="lastName">
<column name="lastname"/>
</property>
<property name="email">
<column name="email"/>
</property>
</class>
</hibernate-mapping>

And this is the jsp file

<%@ page import="org.hibernate.Session" %>
<%@ page import="org.hibernate.SessionFactory" %>
<%@ page import="org.hibernate.cfg.Configuration" %>
<%@ page import="org.hibernate.Transaction" %>
<%@ page import="com.test.hibernate.*" %>

<%

Session session1 = null;

SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
session1 = sessionFactory.openSession();

out.println("Inserting a Record");
Transaction t = session1.beginTransaction();
Contact contact = new Contact();

contact.setId(3);
contact.setFirstName("Deepak");
contact.setLastName("Kumar");
contact.setEmail("deepak_38@yahoo.com");
session1.save(contact);

t.commit();
out.println("Done");
%>

Is there wrong code in my program.

Please any body give solution for my problem

Thanks
&
Regards
Saravanamuthu
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic