Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Connection to database Exception

 
ragi singh
Ranch Hand
Posts: 198
Oracle Suse
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1616
5
Android Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
String url="jdbcracle:thin:@localhost:1521:scott";

That scott above should be the database name, not the user id.
 
ragi singh
Ranch Hand
Posts: 198
Oracle Suse
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i changed the url and username and password and still the exception stays.

Changes are:-
 
Swastik Dey
Rancher
Posts: 1616
5
Android Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From the sql plus window, enter the following in the user id field test/test@dbname. Does it connect?
 
ragi singh
Ranch Hand
Posts: 198
Oracle Suse
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
swastik,

I am unable to connect to my database using the username and password
 
Swastik Dey
Rancher
Posts: 1616
5
Android Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
user id:test
password:test
host string:dbname

does this work?
 
Swastik Dey
Rancher
Posts: 1616
5
Android Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A small modification, in the connection string it's not exactly the dbname, rather it's the service id.
 
ragi singh
Ranch Hand
Posts: 198
Oracle Suse
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what does a service id mean
in tnsnames.ora ?
I cant understand
 
ragi singh
Ranch Hand
Posts: 198
Oracle Suse
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but if i only give the SID how will the database name be found
 
Swastik Dey
Rancher
Posts: 1616
5
Android Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1616
5
Android Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sid itself is mapped to a particular database instance.
 
Swastik Dey
Rancher
Posts: 1616
5
Android Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you go to administrative tools-services, you should get the names of oracle services running.
 
ragi singh
Ranch Hand
Posts: 198
Oracle Suse
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1616
5
Android Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2597
12
C++ Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Swastik Dey
Rancher
Posts: 1616
5
Android Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2597
12
C++ Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1616
5
Android Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks
In that case what comes in place of question mark?

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

 
Jan Cumps
Bartender
Posts: 2597
12
C++ Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic