Solved

JDBC Oracle connection in java

Posted on 2007-03-19
7
639 Views
Last Modified: 2010-05-18
I have connection to Oracle database in ASP. Now, I have problem converting to the connection to use JDBC in java. Please help shed some light.

Oracle Connection in ASP
------------------------------
strConn ="DRIVER={Oracle in OraHome90};DSN=<DSN>;DBQ=<DBQ>;UID=<user name>;PWD=<password>;"
Set conntemp = Server.CreateObject("ADODB.Connection")
if conntemp.state=0 then
      conntemp.Open strConn

My attempt in JDBC
---------------------
String driver = "oracle.jdbc.driver.OracleDriver";
Class.forName(driver);

DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
Connection con = DriverManager.getConnection("jdbc:oracle:thin:@(description=(address=(host=<host name>)(protocol=tcp)(port=<port>))(connect_data=(sid=<sid>)))",<user name>,<password>);

When i run the connection in java, i get IOException : The network adapter could not establish the connection.

Is it because i specify the host name wrongly?

thanks
                  
0
Comment
Question by:rospcc
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 8

Accepted Solution

by:
mnrz earned 125 total points
ID: 18746655
@(description=(address=(host=<host name>)(protocol=tcp)(port=<port>))(connect_data=(sid=<sid>)))

host name, port and sid should be specified

0
 
LVL 92

Assisted Solution

by:objects
objects earned 125 total points
ID: 18746744
0
 
LVL 48

Assisted Solution

by:schwertner
schwertner earned 125 total points
ID: 18747273
I use this with Jdeveloper,
but before this i  place i CLASSPATH the needed
Oracle classes - this is very important


***********************************************************
* Reading from scot/tiger table
**********************************************************

import java.sql.*;

import oracle.jdbc.pool.OracleDataSource;

class first {
public static void main (String args []) throws SQLException {
// Create DataSource and connect to the local database
OracleDataSource ods = new OracleDataSource();
ods.setURL("jdbc:oracle:thin:@//oralin3:1521/phr7b");
ods.setUser("scott");
ods.setPassword("tiger");
Connection conn = ods.getConnection();
// Query the employee names
Statement stmt = conn.createStatement ();
ResultSet rset = stmt.executeQuery ("SELECT ename FROM emp ORDER BY ename");
// Print the name out
while (rset.next ())
System.out.println (rset.getString (1));
//close the result set, statement, and the connection
rset.close();
stmt.close();
conn.close();
}
}
0
 
LVL 30

Assisted Solution

by:Mayank S
Mayank S earned 125 total points
ID: 18750329
Sounds like the machine name or port number you entered in your connection string was incorrect. Are you able to connect through SQL * Plus? Your connection string could also be simplified based on what schwertner mentioned.

>> Class.forName(driver);
>> DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());

The call to registerDriver () is redundant.
0
 
LVL 48

Expert Comment

by:schwertner
ID: 18754579
Also you need in CLASSPATH variable the Oracle classes for connection to the database.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
Shell script to create broker configuration file using current broker Configuration, solely for purpose of backup on Linux. Script may need to be modified depending on OS-installation. Please deploy and verify the script in a test environment.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question