This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes Object Relational Mapping and the fly likes org.hibernate.exception.GenericJDBCException: Cannot open connection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "org.hibernate.exception.GenericJDBCException: Cannot open connection" Watch "org.hibernate.exception.GenericJDBCException: Cannot open connection" New topic
Author

org.hibernate.exception.GenericJDBCException: Cannot open connection

umesh annegirikar
Greenhorn

Joined: May 09, 2011
Posts: 24
Hello Team
I am trying to develop new web application using Struts 2.0,Hibernate,JSP whenever i am compiling this example everything goes well but when i have entered all the values and clicking the save button then it shows the following exception. I have created the database allcontacts in that i have created the contacts table and i have given the reference of that table in my mapping.hbm.xml

org.hibernate.exception.GenericJDBCException: Cannot open connection
org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:126)
org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:114)
org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:52)
org.hibernate.jdbc.ConnectionManager.openConnection(ConnectionManager.java:449)
org.hibernate.jdbc.ConnectionManager.getConnection(ConnectionManager.java:167)
org.hibernate.jdbc.JDBCContext.connection(JDBCContext.java:142)
org.hibernate.transaction.JDBCTransaction.begin(JDBCTransaction.java:85)
org.hibernate.impl.SessionImpl.beginTransaction(SessionImpl.java:1353)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
java.lang.reflect.Method.invoke(Method.java:597)
org.hibernate.context.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:342)
$Proxy63.beginTransaction(Unknown Source)
net.viralpatel.contact.controller.ContactManager.list(ContactManager.java:36)
net.viralpatel.contact.view.ContactAction.add(ContactAction.java:36)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
java.lang.reflect.Method.invoke(Method.java:597)
com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:441)
com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:280)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:243)
com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:165)
com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:252)
org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept(AnnotationValidationInterceptor.java:68)
com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)





Following is my mapping.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE unspecified PUBLIC "//Hibernate/Hibernate Mapping DTD3.0//EN" "http://hibernate-mapping-3.0.dtd" >
<hibernate-mapping>
<class-mapping name="net.viralpatel.contact.model" table="contacts" lazy="false">
<id name="id" column="Id" type="int">
<genarator class="native"/>
</id>
<property name="firstName" column="firstname" length="20" type="string"/>
<property name="lastName" column="lastname" length="80" type="string"/>
<property name="emailId" column="cell" length="30" type="string"/>
<property name="cellNo" column="email" length="50" type="string"/>
<property name="birthDate" column="birthdate" type="date"/>
<property name="website" column="website" length="20" type="string"/>
<property name="created" column="created" type="date"/>
</class-mapping>
</hibernate-mapping>

[/size]


Following is my hibernate.cfg.xml
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>

<session-factory>

<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/AllContacts</property>
<property name="connection.username">root</property>
<property name="connection.password">uha123</property>

<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>

<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>

<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>

<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>

<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>

<!-- Drop and re-create the database schema on startup -->
<!-- <property name="hbm2ddl.auto">update</property>-->

<mapping class="net.viralpatel.contact.model.Contact" />

</session-factory>

</hibernate-configuration>

ContactAction.java

ContactManager.java

package net.viralpatel.contact.controller;


import java.util.List;


import org.hibernate.HibernateException;
import org.hibernate.classic.Session;

import net.viralpatel.contact.model.Contact;
import net.viralpatel.contact.util.HibernateUtil;

public class ContactManager extends HibernateUtil {

public Contact add(Contact contact) {
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
session.save(contact);
session.getTransaction().commit();
return contact;
}
public Contact delete(Long id) {
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
Contact contact = (Contact) session.load(Contact.class, id);
if(null != contact) {
session.delete(contact);
}
session.getTransaction().commit();
return contact;
}

public List<Contact> list() {

Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
List<Contact> contacts = null;
try {

contacts = (List<Contact>)session.createQuery("from Contacts").list();

} catch (HibernateException e) {
e.printStackTrace();
session.getTransaction().rollback();
}
session.getTransaction().commit();
return contacts;
}
}


package net.viralpatel.contact.view;

import java.util.List;

import net.viralpatel.contact.controller.ContactManager;
import net.viralpatel.contact.model.Contact;

import com.opensymphony.xwork2.ActionSupport;


public class ContactAction extends ActionSupport {

private static final long serialVersionUID = 9149826260758390091L;
private Contact contact;
private List<Contact> contactList;
private Long id;

private ContactManager linkController;

public ContactAction() {
linkController = new ContactManager();
}

public String execute() {
this.contactList = linkController.list();
return SUCCESS;
}

public String add() {
System.out.println(getContact());
try {
linkController.add(getContact());
} catch (Exception e) {
e.printStackTrace();
}
this.contactList = linkController.list();
return SUCCESS;
}

public String delete() {
linkController.delete(getId());
return SUCCESS;
}

public Contact getContact() {
return contact;
}

public List<Contact> getContactList() {
return contactList;
}

public void setContact(Contact contact) {
this.contact = contact;
}

public void setContactList(List<Contact> contactsList) {
this.contactList = contactsList;
}

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}
}


HibernateUtil.java
package net.viralpatel.contact.util;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;

public class HibernateUtil {

private static final SessionFactory sessionFactory = buildSessionFactory();

private static SessionFactory buildSessionFactory() {
try {
// Create the SessionFactory from hibernate.cfg.xml
return new AnnotationConfiguration().configure()
.buildSessionFactory();
} catch (Throwable ex) {
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}

public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}


package net.viralpatel.contact.model;

import java.io.Serializable;
import java.sql.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="contacts")
public class Contact implements Serializable{

private static final long serialVersionUID = -8767337896773261247L;

private Long id;
private String firstName;
private String lastName;
private String emailId;
private String cellNo;
private Date birthDate;
private String website;

private Date created;

@Id
@GeneratedValue
@Column(name="Id")
public Long getId() {
return id;
}
@Column(name="firstname")
public String getFirstName() {
return firstName;
}
@Column(name="lastname")
public String getLastName() {
return lastName;
}
@Column(name="email")
public String getEmailId() {
return emailId;
}
@Column(name="cell")
public String getCellNo() {
return cellNo;
}
@Column(name="birthdate")
public Date getBirthDate() {
return birthDate;
}
@Column(name="website")
public String getWebsite() {
return website;
}
@Column(name="created")
public Date getCreated() {
return created;
}
public void setId(Long id) {
this.id = id;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public void setEmailId(String emailId) {
this.emailId = emailId;
}
public void setCellNo(String cellNo) {
this.cellNo = cellNo;
}
public void setBirthDate(Date birthDate) {
this.birthDate = birthDate;
}
public void setCreated(Date created) {
this.created = created;
}
public void setWebsite(String website) {
this.website = website;
}
}

Please give me the guadance
Thanks and Regards
Umesh
Volodymyr Levytskyi
Ranch Hand

Joined: Mar 29, 2012
Posts: 481
    
    1

Hello!

Maybe you need to change database name in connection.url in hibernate.cfg.xml to real case-sensitive name.


True person is moral, false is right!
umesh annegirikar
Greenhorn

Joined: May 09, 2011
Posts: 24
Volodymyr Levytskyi wrote:Hello!

Maybe you need to change database name in connection.url in hibernate.cfg.xml to real case-sensitive name.


Hello Friend
I have also change this but it still shows the same exception

Thanks ,
Umesh Annegirikar
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: org.hibernate.exception.GenericJDBCException: Cannot open connection
 
Similar Threads
Hibernate
problem regarding database access using hibernate
QuerySyntaxException (table is not mapped)
org.hibernate.exception.GenericJDBCException
Getting an exception in Hibernate as I try to integrate Struts2 and Hibernate3