wood burning stoves 2.0*
The moose likes JDBC and the fly likes Connection to database Exception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Connection to database Exception" Watch "Connection to database Exception" New topic
Author

Connection to database Exception

ragi singh
Ranch Hand

Joined: Mar 10, 2010
Posts: 198

Hi

I am trying to connect to oracle database in my application however i get the following exception
Inside the second try block
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:413)
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:582)
at java.sql.DriverManager.getConnection(DriverManager.java:185)
at Dbconnection.main(Dbconnection.java:17)

and my code is as follows



I am unable to follow where the error is occuring
Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1470
    
    6

String url="jdbcracle:thin:@localhost:1521:scott";

That scott above should be the database name, not the user id.


Swastik
ragi singh
Ranch Hand

Joined: Mar 10, 2010
Posts: 198

i changed the url and username and password and still the exception stays.

Changes are:-
Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1470
    
    6

From the sql plus window, enter the following in the user id field test/test@dbname. Does it connect?
ragi singh
Ranch Hand

Joined: Mar 10, 2010
Posts: 198

swastik,

I am unable to connect to my database using the username and password
Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1470
    
    6

user id:test
password:test
host string:dbname

does this work?
Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1470
    
    6

A small modification, in the connection string it's not exactly the dbname, rather it's the service id.
ragi singh
Ranch Hand

Joined: Mar 10, 2010
Posts: 198

what does a service id mean
in tnsnames.ora ?
I cant understand
ragi singh
Ranch Hand

Joined: Mar 10, 2010
Posts: 198

but if i only give the SID how will the database name be found
Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1470
    
    6

So far as I know using a sid a particular oracle instance is identified, i.e. the address where it's located, protocol being used etc.
Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1470
    
    6

sid itself is mapped to a particular database instance.
Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1470
    
    6

If you go to administrative tools-services, you should get the names of oracle services running.
ragi singh
Ranch Hand

Joined: Mar 10, 2010
Posts: 198

my database is MyDatabase .
After the given url used for connecting to the database ,
ie the url with SID how will a particular schema be called ?
Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1470
    
    6

Each particular sid is mapped to a particular db instance, and accordingly you have to configure listeners in tnsnames.ora. For e.g. if you have a db instance running as orcl, you may configure a sid in tnsnames.ora as orcl, and if you have another db instance running as mydatabase, you need to configure a sid for this too.
Jan Cumps
Bartender

Joined: Dec 20, 2006
Posts: 2497
    
    8

Swastik Dey wrote:Each particular sid is mapped to a particular db instance, and accordingly you have to configure listeners in tnsnames.ora. For e.g. if you have a db instance running as orcl, you may configure a sid in tnsnames.ora as orcl, and if you have another db instance running as mydatabase, you need to configure a sid for this too.
Are you talking about the server side, or the client side?


OCUP UML fundamental and ITIL foundation
youtube channel
Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1470
    
    6

I haven't worked much with oracle. But as far as my knowledge goes tnsnames.ora is configured at client side. Correct me if I am wrong.
Jan Cumps
Bartender

Joined: Dec 20, 2006
Posts: 2497
    
    8

Swastik Dey wrote:I haven't worked much with oracle. But as far as my knowledge goes tnsnames.ora is configured at client side. Correct me if I am wrong.
If you use the thin client, no tnsnames configuration on client side is required.
Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1470
    
    6

Thanks
In that case what comes in place of question mark?

jdbc:oracle:thin:@localhost:1521:?

Jan Cumps
Bartender

Joined: Dec 20, 2006
Posts: 2497
    
    8

from http://blogs.oracle.com/theshortenspot/2009/04/advanced_oracle_jdbc_connectio.html

jdbc:oracle:thin:@[hostname]:[dbport]:[database]

where
[hostname] - Database hostname
[dbport] - Database Listener portname
[database] - Database Name

[edit: disabled smilies]
 
Don't get me started about those stupid light bulbs.
 
subject: Connection to database Exception