aspose file tools*
The moose likes JDBC and the fly likes How to connect oracle 11g r1 database with java program. 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 "How to connect oracle 11g r1 database with java program." Watch "How to connect oracle 11g r1 database with java program." New topic
Author

How to connect oracle 11g r1 database with java program.

Abhishek Nayak
Greenhorn

Joined: Feb 12, 2013
Posts: 2
Hi,
i am trying to connect oracle 11g with my java program in eclipse IDE, and i have added ojdbc6.jar in class path.
Databse Name: Oracle 11g r1 Enterprise edition.
operating system: windows 7 64bit.

my code is:



above program throws below exception:

java.sql.SQLException: The Network Adapter could not establish the connection
at oracle.jdbc.driver.SQLStateMapping.newSQLException(SQLStateMapping.java:70)
at oracle.jdbc.driver.DatabaseError.newSQLException(DatabaseError.java:133)
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:199)
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:480)
at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:413)
at oracle.jdbc.driver.PhysicalConnection.<init>(PhysicalConnection.java:508)
at oracle.jdbc.driver.T4CConnection.<init>(T4CConnection.java:203)
at oracle.jdbc.driver.T4CDriverExtension.getConnection(T4CDriverExtension.java:33)
at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:510)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at abhishek.Connect.main(Connect.java:12)
Caused by: oracle.net.ns.NetException: The Network Adapter could not establish the connection
at oracle.net.nt.ConnStrategy.execute(ConnStrategy.java:328)
at oracle.net.resolver.AddrResolution.resolveAndExecute(AddrResolution.java:421)
at oracle.net.ns.NSProtocol.establishConnection(NSProtocol.java:630)
at oracle.net.ns.NSProtocol.connect(NSProtocol.java:206)
at oracle.jdbc.driver.T4CConnection.connect(T4CConnection.java:966)
at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:292)
... 7 more
Caused by: java.net.UnknownHostException: //localhost
at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at oracle.net.nt.TcpNTAdapter.connect(TcpNTAdapter.java:127)
at oracle.net.nt.ConnOption.connect(ConnOption.java:126)
at oracle.net.nt.ConnStrategy.execute(ConnStrategy.java:306)
... 12 more

can anyone tell me what's wrong am doing?
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

Are you able to connect to the DB via Oracle directly using same credentials and is the test connection successful? Secondly, what is the name of DB that you created after installation of Oracle?


~ Mansukh
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38412
    
  23
I think this discussion would fit better on one of our database fora. Moving.
chris webster
Bartender

Joined: Mar 01, 2009
Posts: 1658
    
  14

You gave your DB URL as: "jdbc:oracle:thin:@//localhost:1158:v$instance"

The port 1158 is usually the port that the Oracle Enterprise Manager application listens on, so you won't be able to use it for a JDBC connection. The default port for the database listener is 1521. Try that and see if it works.

Also, the usual structure of an Oracle JDBC URL is something like "jdbc:oracle:thin:@myhost:1521:orcl" where your DB's name is "orcl" (this is the usual default), so you should also check that you are providing the right DB name in the right format.

You could check in your TNSNAMES.ORA file (this tells Oracle clients like SQL*Plus how to connect to the DB) to find the port and name of your target database, and make sure you use those details in a valid JDBC URL.


No more Blub for me, thank you, Vicar.
Vinod Tiwari
Ranch Hand

Joined: Feb 06, 2008
Posts: 459
    
    1
I hope you must have resolved the issue by now, if not, you might want to check if database is installed on your local machine or not. Some issue with connection parameters only. Check port and other details pointed in previous comments.

Vinod
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to connect oracle 11g r1 database with java program.