• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

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

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,


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

Warm Regards,
Prasanna Lakshmi Tallapaka
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you show us your code and post the stack trace?
 
Prasanna Lakshmi Tallapaka
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Prasanna Lakshmi Tallapaka
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Please check "hibernate.connection.URL" in your configuration file.
 
Prasanna Lakshmi Tallapaka
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sorry,

My url looks something like :



Still I see the same exception.

Warm Regards,
Prasanna Laskhmi Tallapaka
 
Sridhar Gudipalli
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 672
4
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Check the below link, it might help..
https://coderanch.com/t/307103/JDBC/java/java-sql-SQLException-Io-exception
 
Without deviation from the norm, progress is not possible - Zappa. Tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic