Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / September 2006

Tip: Looking for answers? Try searching our database.

Oracle JDBC Problem

Thread view: 
jctown@nb.sympatico.ca - 04 Sep 2006 15:20 GMT
I have a Java program on a laptop which is trying to access an Oracle
XE Database on my desktop.

Here is the error I get...obviously there is something wrong with a
connection string or something but I don't know what it would be

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:111)
    at
oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:145)
    at
oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:254)
    at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:386)
    at
oracle.jdbc.driver.PhysicalConnection.<init>(PhysicalConnection.java:419)
    at oracle.jdbc.driver.T4CConnection.<init>(T4CConnection.java:164)
    at
oracle.jdbc.driver.T4CDriverExtension.getConnection(T4CDriverExtension.java:34)
    at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:752)
    at java.sql.DriverManager.getConnection(DriverManager.java:525)
    at java.sql.DriverManager.getConnection(DriverManager.java:171)
    at Oracle.OracleTest.main(OracleTest.java:12)

And here is my source

package Oracle;

import java.lang.*;
import java.util.*;
import java.sql.*;

class OracleTest {
 public static void main (String args []) throws SQLException
 {
       DriverManager.registerDriver (new
oracle.jdbc.driver.OracleDriver());

       Connection conn = DriverManager.getConnection
            ("jdbc:oracle:thin:@emily5:1521:XE", "xxxxxx", "######"");
                            // @machineName:port:SID,   userid,
password

       Statement stmt = conn.createStatement();
       ResultSet rset = stmt.executeQuery("select * from books");
       while (rset.next())
       {
           int index = 1;
               while (index <= 7)
               {
                       System.out.print (rset.getString(index) + " ");
                   index++;
               }
               System.out.println ("");
       }
       stmt.close();

 }
}

The program compiles and I have the odbc jar on the class path.
Rohit Kumbhar - 04 Sep 2006 19:01 GMT
> [...]
>
> Exception in thread "main" java.sql.SQLException: Io exception: The
> Network Adapter could not establish the connection

Can you ping your desktop machine from your laptop?
jctown@nb.sympatico.ca - 04 Sep 2006 20:01 GMT
> > [...]
> >
> > Exception in thread "main" java.sql.SQLException: Io exception: The
> > Network Adapter could not establish the connection
>
> Can you ping your desktop machine from your laptop?

yup...  using both alias and ip
Juha Laiho - 04 Sep 2006 20:07 GMT
"jctown@nb.sympatico.ca" <jctown@nb.sympatico.ca> said:
>I have a Java program on a laptop which is trying to access an Oracle
>XE Database on my desktop.
...
>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:111)
...
>    at Oracle.OracleTest.main(OracleTest.java:12)

Ok, the error tells a number of things:
- you got the proper drivers loaded (Oracle JDBC, not ODBC)
- the database JDBC URL is at least correct enough that the
 driver attempted to connect
- the connection attempt failed

>package Oracle;
>
[quoted text clipped - 9 lines]
>        Connection conn = DriverManager.getConnection
>             ("jdbc:oracle:thin:@emily5:1521:XE", "xxxxxx", "######"");

So, it fails here. Looking ad the JDBC URL, it seems correct, assuming that
"emily5" is the name (in TCP/IP DNS, not just an arbitary computer name)
of the desktop running the database. As someone else responded, try pinging
that name, to see if it responds.

Other things, are you running Oracle TNS Listener service on emily5?
Is the listener service listening on port 1521?

You could find out these with commands
lsnrctl status
netstat -a
on the database server machine.

>            int index = 1;
>                while (index <= 7)
>                {
>                        System.out.print (rset.getString(index) + " ");
>                    index++;
>                }

As an aside, this could be more naturally written as a for loop:
for (int index = 1; index <= 7; index++) {
    System.out.print(rset.getString(index) + " ");
}

>                System.out.println ("");
>        }
>        stmt.close();

... and in real programs, remember to also close the connection:

conn.close();

Signature

Wolf  a.k.a.  Juha Laiho     Espoo, Finland
(GC 3.0) GIT d- s+: a C++ ULSH++++$ P++@ L+++ E- W+$@ N++ !K w !O !M V
        PS(+) PE Y+ PGP(+) t- 5 !X R !tv b+ !DI D G e+ h---- r+++ y++++
"...cancel my subscription to the resurrection!" (Jim Morrison)

jctown@nb.sympatico.ca - 04 Sep 2006 21:15 GMT
> "jctown@nb.sympatico.ca" <jctown@nb.sympatico.ca> said:
> >I have a Java program on a laptop which is trying to access an Oracle
[quoted text clipped - 17 lines]
> of the desktop running the database. As someone else responded, try pinging
> that name, to see if it responds.

No problem here:

F:\>ping emily5

Pinging emily5 [192.168.0.100] with 32 bytes of data:

Reply from 192.168.0.100: bytes=32 time=1ms TTL=128
Reply from 192.168.0.100: bytes=32 time=1ms TTL=128
Reply from 192.168.0.100: bytes=32 time=1ms TTL=128
Reply from 192.168.0.100: bytes=32 time=1ms TTL=128

Ping statistics for 192.168.0.100:
   Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
   Minimum = 1ms, Maximum = 1ms, Average = 1ms

> Other things, are you running Oracle TNS Listener service on emily5?
> Is the listener service listening on port 1521?
[quoted text clipped - 3 lines]
> netstat -a
> on the database server machine.

Here are the results of the execution of the above two commands on the
database server machine.

J:\> lsnrctl status

LSNRCTL for 32-bit Windows: Version 10.2.0.1.0 - Production on
04-SEP-2006 17:11
:27

Copyright (c) 1991, 2005, Oracle.  All rights reserved.

Connecting to
(DESCRIPTION=(ADDRESS=(PROTOCOL=IPC)(KEY=EXTPROC_FOR_XE)))
STATUS of the LISTENER
------------------------
Alias                     LISTENER
Version                   TNSLSNR for 32-bit Windows: Version
10.2.0.1.0 - Produ
ction
Start Date                01-SEP-2006 18:52:11
Uptime                    2 days 22 hr. 19 min. 15 sec
Trace Level               off
Security                  ON: Local OS Authentication
SNMP                      OFF
Default Service           XE
Listener Parameter File
C:\oraclexe\app\oracle\product\10.2.0\server\network\a
dmin\listener.ora
Listener Log File
C:\oraclexe\app\oracle\product\10.2.0\server\network\l
og\listener.log
Listening Endpoints Summary...

(DESCRIPTION=(ADDRESS=(PROTOCOL=ipc)(PIPENAME=\\.\pipe\EXTPROC_FOR_XEipc)))
 (DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=Emily5)(PORT=1521)))

(DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=Emily5)(PORT=8081))(Presentation=HTT
P)(Session=RAW))
Services Summary...
Service "CLRExtProc" has 1 instance(s).
 Instance "CLRExtProc", status UNKNOWN, has 1 handler(s) for this
service...
Service "PLSExtProc" has 1 instance(s).
 Instance "PLSExtProc", status UNKNOWN, has 1 handler(s) for this
service...
Service "XEXDB" has 1 instance(s).
 Instance "xe", status READY, has 1 handler(s) for this service...
Service "XE_XPT" has 1 instance(s).
 Instance "xe", status READY, has 1 handler(s) for this service...
Service "xe" has 1 instance(s).
 Instance "xe", status READY, has 1 handler(s) for this service...
The command completed successfully

J:\>

J:\> netstat -a

Active Connections  (only 1521 is displayed to save bandwidth)

 Proto  Local Address          Foreign Address        State

 TCP    Emily5:1521            Emily5:0               LISTENING
---------------------------------------------------------------------
It looks like everything is fine on both sides so that's why I asked
the original question.
I suspect it has something to do with the tns names file which I dont
have on the remote box,
Juha Laiho - 05 Sep 2006 18:52 GMT
"jctown@nb.sympatico.ca" <jctown@nb.sympatico.ca> said:
>> "jctown@nb.sympatico.ca" <jctown@nb.sympatico.ca> said:
>> >I have a Java program on a laptop which is trying to access an Oracle
[quoted text clipped - 12 lines]
>>   driver attempted to connect
>> - the connection attempt failed

(much data snipped)

>It looks like everything is fine on both sides so that's why I asked
>the original question.

Yep, everything seems to be in order. How about a host-based firewall
on either of the machines? Can you telnet from the client to port
1521 on the server?

>I suspect it has something to do with the tns names file which I dont
>have on the remote box,

No, Oracle JDBC doesn't require tnsnames; I recall it can utilise
tnsnames, but then the URL syntax would be different.
Signature

Wolf  a.k.a.  Juha Laiho     Espoo, Finland
(GC 3.0) GIT d- s+: a C++ ULSH++++$ P++@ L+++ E- W+$@ N++ !K w !O !M V
        PS(+) PE Y+ PGP(+) t- 5 !X R !tv b+ !DI D G e+ h---- r+++ y++++
"...cancel my subscription to the resurrection!" (Jim Morrison)

jctown@nb.sympatico.ca - 05 Sep 2006 21:31 GMT
Massive snippage follows------
> Yep, everything seems to be in order. How about a host-based firewall
> on either of the machines? Can you telnet from the client to port
> 1521 on the server?

Don't laugh at me... but it was my XP Firewall that was blocking it.
Once I opened up port 1521 on my server I had no problems...   thanks
for your help

> >have on the remote box,
>
[quoted text clipped - 5 lines]
>          PS(+) PE Y+ PGP(+) t- 5 !X R !tv b+ !DI D G e+ h---- r+++ y++++
> "...cancel my subscription to the resurrection!" (Jim Morrison)
Greg R. Broderick - 05 Sep 2006 02:05 GMT
>>                System.out.println ("");
>>        }
[quoted text clipped - 3 lines]
>
> conn.close();

Preferrably in a finally clause, so that it is executed even if the code
throws an exception!

Cheers
GRB

Signature

---------------------------------------------------------------------
Greg R. Broderick            gregb.usenet200607@blackholio.dyndns.org

A. Top posters.
Q. What is the most annoying thing on Usenet?
---------------------------------------------------------------------

Daniel Dyer - 04 Sep 2006 20:38 GMT
> I have a Java program on a laptop which is trying to access an Oracle
> XE Database on my desktop.
>
> Here is the error I get...obviously there is something wrong with a
> connection string or something but I don't know what it would be

...

>         Connection conn = DriverManager.getConnection
>              ("jdbc:oracle:thin:@emily5:1521:XE", "xxxxxx", "######"");
>                              // @machineName:port:SID,   userid,
> password

My (working) Oracle JDBC URLs have the SID separated from the port with a  
forward slash, like this:

    jdbc:oracle:thin:@emily5:1521/XE

Dan.

Signature

Daniel Dyer
http://www.dandyer.co.uk

jctown@nb.sympatico.ca - 04 Sep 2006 21:23 GMT
> > I have a Java program on a laptop which is trying to access an Oracle
> > XE Database on my desktop.
[quoted text clipped - 19 lines]
> Daniel Dyer
> http://www.dandyer.co.uk

Hi, I changed the separator but got the same error
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:111)
    at
oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:145)
    at
oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:254)
    at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:386)
    at
oracle.jdbc.driver.PhysicalConnection.<init>(PhysicalConnection.java:419)
    at oracle.jdbc.driver.T4CConnection.<init>(T4CConnection.java:164)

I'm using Eclipse 3.2


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.