• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

JDBC- ORACLE

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,
I am getting below exception while trying to connect ORACLE database using jdbc racle:thin driver:
Exception in thread "main" java.sql.SQLException: Io exception: The Network Adap
ter could not establish the connection
at oracle.jdbc.dbaccess.DBError.throwSqlException(DBError.java:114)
at oracle.jdbc.dbaccess.DBError.throwSqlException(DBError.java:156)
at oracle.jdbc.dbaccess.DBError.throwSqlException(DBError.java:269)
at oracle.jdbc.driver.OracleConnection.<init>(OracleConnection.java:210)
at oracle.jdbc.driver.OracleDriver.getConnectionInstance(OracleDriver.ja
va:251)
at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:224)
at java.sql.DriverManager.getConnection(Compiled Code)
at java.sql.DriverManager.getConnection(DriverManager.java:137)
at Employee1.main(Compiled Code)

If anyone can give me reason for the above exception. It would be a great help to me.
TIA
Praveen Kumar
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have had a similar problem. What I did first was verify that I could connect to Oracle. We use TNS names so I went to a DOS prompt and entered TNSPING <dbinstance> to verify the database connection properties. I then wrote down the hostname from here and the port.
To double check that my computer could connect to Oracle, I used sqlplus to verify my connection. If you cannot connect to Oracle via SQLPlus, maybe you have other restrictions you are not aware of (firewall, listener problem, etc).
I am using the JDBC thin-client from Oracle (classes111.zip) and here is an example. I read from a flat-file to get the setting information so the connection information is not hardcoded:
//OracleFIXJDBC=oracle.jdbc.driver.OracleDriver
//OracleFIXURL=jdbc racle:thin:@myhost.sonetpremier.com:1521:HOMEDB
//WhichJDBC=OracleFIX

private String determineJDBCDriver(){
String whichJDBC = getRunProperty("WhichJDBC");
String jdbcDriver = getRunProperty((whichJDBC+"JDBC"));
//new oracle.jdbc.driver.OracleDriver()
try{
DriverManager.registerDriver((Driver)Class.forName(jdbcDriver).newInstance());
addTrace("Driver "+jdbcDriver+" is now registered");
} catch (SQLException sqle){
addTrace("A SQL Exception occurred -> "+sqle);
return null;
} catch (ClassNotFoundException cnfe){
addTrace("JDBC Driver "+jdbcDriver+" not found -->"+cnfe);
return null;
} catch (Exception e){
addTrace("An Exception occurred while setting JDBC Driver -> "+e);
return null;
}
String getUrl = whichJDBC+"URL";
if(getUrl != null)
addTrace("Getting JDBC URL with -> "+getUrl);
return getRunProperty(getUrl);
}

public boolean authenticateUser()
{
try{
String jdbcConnection = determineJDBCDriver();//getRunProperty("JDBCConnection");
if(jdbcConnection==null){
return false;
} else {
addTrace("Received JDBC URL -> "+jdbcConnection);
}
String user=getRunProperty("DefaultUser");
String pass=getRunProperty("DefaultPassword");

addTrace("JDBC -> "+jdbcConnection+" and USER -> "+user+" and PASS -> "+pass);
java.sql.Connection jdbcConn =
DriverManager.getConnection(jdbcConnection,user,pass);

String sSQL = "SELECT ORACLE_USERNAME FROM SEC_USER WHERE UPPER(ORACLE_USERNAME) ='" + getLogin().trim().toUpperCase() + "'";
addTrace("authenticateUser theSQL->" + sSQL);

Statement stmt = jdbcConn.createStatement();
boolean value = false;
try{
java.sql.ResultSet rset = stmt.executeQuery(sSQL);
while(rset.next()){
//addTrace("Found User ->"+rset.getString(1));
value = true;
}
stmt.close();
jdbcConn.close();
return value;
} catch (Exception e){
addTrace("User "+getLogin().trim()+" not found");
return false;
}
}catch(Exception ex){
addTrace("Error in authenticateUser "+ex.toString());
return false;
}
}
James
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic