GeeCON Prague 2014*
The moose likes Object Relational Mapping and the fly likes Exception in thread Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Exception in thread "main" org.hibernate.exception.GenericJDBCException: Cannot open connection" Watch "Exception in thread "main" org.hibernate.exception.GenericJDBCException: Cannot open connection" New topic
Author

Exception in thread "main" org.hibernate.exception.GenericJDBCException: Cannot open connection

Prasanna Lakshmi Tallapaka
Greenhorn

Joined: Jun 28, 2010
Posts: 24
Hi,


Could any one be so kind to help me getting rid of this exception. Many thanks!

Warm Regards,
Prasanna Lakshmi Tallapaka
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Can you show us your code and post the stack trace?


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Prasanna Lakshmi Tallapaka
Greenhorn

Joined: Jun 28, 2010
Posts: 24
many thanks for your response.

Here you go!

My Hibernate Client program.
EmployeeDetailsClient.java.

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

import com.myhibernate.hp.util.HibernateUtil;

public class EmployeeDetailsClient {
public static void main(String[] args) {
Session session = null;

try{
// This step will read hibernate.cfg.xml and prepare hibernate for use
session = HibernateUtil.getSessionFactory().openSession();
//Transaction transaction = null;

//SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
//session =sessionFactory.openSession();
//Create new instance of EmployeeDetails and set values in it by reading them from form object
System.out.println("Inserting Record");
EmployeeDetails employeeDetails=new EmployeeDetails();

employeeDetails.setEmpName("xxxxxx");
employeeDetails.setEmpDept("vvvvvvv");

session.save(employeeDetails);
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();

}

}

}

hibernate.cfg.xml

<?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">oracle.jdbc.driver.OracleDriver</property>
<property name="hibernate.connection.url">jdbcracle:thin:@mydb:db</property>
<property name="hibernate.connection.username">xxx</property>
<property name="hibernate.connection.password">yyy</property>
<property name="hibernate.connection.pool_size">5</property>
<property name="show_sql">true</property>
<property name="dialect">org.hibernate.dialect.OracleDialect</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<!-- Mapping files -->
<mapping resource="employee.hbm.xml"/>
</session-factory>
</hibernate-configuration>

HibernateUtil.java

package com.myhibernate.hp.util;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class HibernateUtil {
private static final SessionFactory sessionFactory;
static {
try {
sessionFactory = new Configuration().configure()
.buildSessionFactory();
} catch (Throwable ex) {
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}

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

employee.hbm.xml

<?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.myhibernate.hp.test.EmployeeDetails" table="employee">
<id name="empNo" type="long" column="empno" >
<generator class="assigned"/>
</id>

<property name="empName">
<column name="empname" />
</property>
<property name="empDept">
<column name="empdept"/>
</property>
</class>
</hibernate-mapping>

Stack trace :

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:419)
at org.hibernate.jdbc.JDBCContext.connect(JDBCContext.java:145)
at org.hibernate.jdbc.JDBCContext.connection(JDBCContext.java:91)
at org.hibernate.jdbc.AbstractBatcher.prepareStatement(AbstractBatcher.java:74)
at org.hibernate.jdbc.AbstractBatcher.prepareStatement(AbstractBatcher.java:67)
at org.hibernate.jdbc.AbstractBatcher.prepareBatchStatement(AbstractBatcher.java:148)
at org.hibernate.persister.entity.BasicEntityPersister.insert(BasicEntityPersister.java:1848)
at org.hibernate.persister.entity.BasicEntityPersister.insert(BasicEntityPersister.java:2209)
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:678)
at com.myhibernate.hp.test.EmployeeDetailsClient.main(EmployeeDetailsClient.java:33)
Caused by: java.sql.SQLException: Io exception: The Network Adapter could not establish the connection
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:125)
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:162)
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:274)
at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:328)
at oracle.jdbc.driver.PhysicalConnection.<init>(PhysicalConnection.java:361)
at oracle.jdbc.driver.T4CConnection.<init>(T4CConnection.java:151)
at oracle.jdbc.driver.T4CDriverExtension.getConnection(T4CDriverExtension.java:32)
at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:595)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at org.hibernate.connection.DriverManagerConnectionProvider.getConnection(DriverManagerConnectionProvider.java:110)
at org.hibernate.jdbc.AbstractBatcher.openConnection(AbstractBatcher.java:416)
... 15 more
Many thanks in advance.

Warm Regards,
Prasanna Lakshmi Tallapaka
Sridhar Gudipalli
Ranch Hand

Joined: Nov 02, 2005
Posts: 120
Caused by: java.sql.SQLException: Io exception: The Network Adapter could not establish the connection
..... at com.myhibernate.hp.test.EmployeeDetailsClient.main(EmployeeDetailsClient.java:33)


Please use code tags so that it is easy to look the exact line number.

Sridhar Gudipalli|SCJP 6.0
SCWCD objectives
Prasanna Lakshmi Tallapaka
Greenhorn

Joined: Jun 28, 2010
Posts: 24
Hi Sridhar,

I am sorry. I could not get you. Could you please help me knowing what are code tags. Many thanks!

Warm Regards,
Prasanna Lakshmi Tallapaka
Sridhar Gudipalli
Ranch Hand

Joined: Nov 02, 2005
Posts: 120
When you paste the code here, select it and press the "Code" button (or also prefix with '[ code ]' and suffix with '[/code]' ). It formats the code properly and puts line numbers.
Prasanna Lakshmi Tallapaka
Greenhorn

Joined: Jun 28, 2010
Posts: 24
Ok. Thanks.

Here you go.

EmployeeDetailsClient.java.




hibernate.cfg.xml




HibernateUtil.java



employee.hbm.xml




Stack Trace

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:419)
at org.hibernate.jdbc.JDBCContext.connect(JDBCContext.java:145)
at org.hibernate.jdbc.JDBCContext.connection(JDBCContext.java:91)
at org.hibernate.jdbc.AbstractBatcher.prepareStatement(AbstractBatcher.java:74)
at org.hibernate.jdbc.AbstractBatcher.prepareStatement(AbstractBatcher.java:67)
at org.hibernate.jdbc.AbstractBatcher.prepareBatchStatement(AbstractBatcher.java:148)
at org.hibernate.persister.entity.BasicEntityPersister.insert(BasicEntityPersister.java:1848)
at org.hibernate.persister.entity.BasicEntityPersister.insert(BasicEntityPersister.java:2209)
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:678)
at com.myhibernate.hp.test.EmployeeDetailsClient.main(EmployeeDetailsClient.java:33)
Caused by: java.sql.SQLException: Io exception: The Network Adapter could not establish the connection
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:125)
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:162)
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:274)
at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:328)
at oracle.jdbc.driver.PhysicalConnection.<init>(PhysicalConnection.java:361)
at oracle.jdbc.driver.T4CConnection.<init>(T4CConnection.java:151)
at oracle.jdbc.driver.T4CDriverExtension.getConnection(T4CDriverExtension.java:32)
at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:595)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at org.hibernate.connection.DriverManagerConnectionProvider.getConnection(DriverManagerConnectionProvider.java:110)
at org.hibernate.jdbc.AbstractBatcher.openConnection(AbstractBatcher.java:416)
... 15 more
Many thanks in advance.

Warm Regards,
Prasanna Lakshmi Tallapaka
Sridhar Gudipalli
Ranch Hand

Joined: Nov 02, 2005
Posts: 120

Please check "hibernate.connection.URL" in your configuration file.
Prasanna Lakshmi Tallapaka
Greenhorn

Joined: Jun 28, 2010
Posts: 24
sorry,

My url looks something like :



Still I see the same exception.

Warm Regards,
Prasanna Laskhmi Tallapaka
Sridhar Gudipalli
Ranch Hand

Joined: Nov 02, 2005
Posts: 120
Are you able to connect to the DB thru JDBC using same credentials? Also can you please modify the finally block as below?

Prasanna Lakshmi Tallapaka
Greenhorn

Joined: Jun 28, 2010
Posts: 24
Hi Sridhar,

Many thanks for your response. I am using TOAD to connect to Oracle. Version being used is 10g. Am able to connect thru Toad. I am trying to connect to the DB remotely and am able to login thru toad. Tried connecting using the JDBC code and am seeing the following exception. The code is mentioned below.

Exception in thread "main" java.sql.SQLException: Io exception: The Network Adapter could not establish the connection
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:125)
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:162)
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:274)
at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:328)
at oracle.jdbc.driver.PhysicalConnection.<init>(PhysicalConnection.java:361)
at oracle.jdbc.driver.T4CConnection.<init>(T4CConnection.java:151)
at oracle.jdbc.driver.T4CDriverExtension.getConnection(T4CDriverExtension.java:32)
at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:595)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at JdbcConnect.main(JdbcConnect.java:39)

The jdbc code would look something like below :



Many thanks for your response and concern.

Warm Regards,
Prasanna Lakshmi Tallapaka
Prasad Krishnegowda
Ranch Hand

Joined: Apr 25, 2010
Posts: 532

Check the below link, it might help..
http://www.coderanch.com/t/307103/JDBC/java/java-sql-SQLException-Io-exception
 
GeeCON Prague 2014
 
subject: Exception in thread "main" org.hibernate.exception.GenericJDBCException: Cannot open connection