LucasSeveryn
LucasSeveryn

Reputation: 6262

Connect to oracle database, knowing only the database name

I have an existing connection in R, where I use DBI library to do the following:

con <- dbConnect(drv, username = "user", password = "pass", dbname = "mydatabase.world")

This creates the following connection:

User name:             user
Connect string:        mydatabase.world
Server version:        11.2.0.4.0 
Server type:           Oracle RDBMS 
Results processed:     0 
OCI prefetch:          FALSE 
Bulk read:             25 
Statement cache size:  0 
LOB prefetch size:     1024 
Open results:          0 

I would like to do the same thing in Java. I browsed lots of tutorials, and arrived at this:

private static Connection getDBConnection() {

Connection dbConnection = null;

try {

    Class.forName(DB_DRIVER);

} catch (ClassNotFoundException e) {

    System.out.println(e.getMessage());

}

try {

    dbConnection = DriverManager.getConnection(DB_CONNECTION, DB_USER,
            DB_PASSWORD);
    return dbConnection;

} catch (SQLException e) {

    System.out.println(e.getMessage());

}

return dbConnection;

}

but I get this error:

The Network Adapter could not establish the connection

I could connect just with database name in R, how to do the same thing in Java?

My current DB_CONNECTION string is:

private static final String DB_CONNECTION = "jdbc:oracle:thin:@mydatabase.world";

tnsping:

Message 3511 not found; No message file for product=NETWORK, facility=TNSMessage
 3512 not found; No message file for product=NETWORK, facility=TNSAttempting to
contact (DESCRIPTION = (ADDRESS_LIST = (ADDRESS = (COMMUNITY = tcp.world)(PROTOC
OL = TCP)(Host = 10.3.0.70)(Port = 1532))) (CONNECT_DATA = (SID = LUCAS)))
Message 3509 not found; No message file for product=NETWORK, facility=TNS'

Solution

For whatever reason, I could not use the database name in Java. Here is what I had to do:

  1. Run tnsping (found in oracle directory, BIN subdirectory) with the database name as parameter

tnsping mydatabase.world

  1. Then from there I got my host name and SID

  2. The string is now:

String DB_CONNECTION = "jdbc:oracle:thin:@10.3.0.70:1532:LUCAS";

Upvotes: 2

Views: 384

Answers (2)

guillaume guerin
guillaume guerin

Reputation: 367

knowing only the database name

I don't really understand, according to your R code it seems to me that DB_USER = "user" and DB_PASSWORD = "pass".

Also did you try adding the port and the SID to your DB_CONNECTION String ?

private static final String DB_CONNECTION = "jdbc:oracle:thin:@10.3.0.70:1532:LUCAS";

I edited my post with your tnsping info.

Upvotes: 1

Darkwing
Darkwing

Reputation: 314

If i understood corectly you are not sure about connection parameters as hosthame and port.
It seems that mydatabase.world is a dsn name. So parameters you needed can be located at dsn: /etc/odbc.ini or /etc/odbcinst.ini

Upvotes: 1

Related Questions