0

I am connecting to Oracle 11g DB trough my java program. I am using Service Name and not SID.

addr = jdbc:oracle:thin:@hostIP:1521:ServiceName
Class.forName("oracle.jdbc.OracleDriver");
Connection con = DriverManager.getConnection(addr,un,pw);
Statement stat = con.createStatement();
ResultSet rs = stat.executeQuery(SELECT * from Table);

This works great. I am able to connect to DB and retrieve the data.

However, if I pass Service ID instead of Service Name, code doesn't work! I get exception. I tried solution mentioned here - Java JDBC - How to connect to Oracle using Service Name instead of SID. But i still see the same exception.

Community
  • 1
  • 1
Shreyas SG
  • 368
  • 3
  • 6
  • 21
  • 1
    The linked question is vice versa. It asks for how to connect using service name instead of SID. You are asking for how to connect using SID instead of service name. The linked question also shows the URL schema for using service names differently as you are using. Sure that you are not using SID already (and only think you are using service name)? – Seelenvirtuose Aug 24 '16 at 06:19
  • Yup. I ensured that I am using Service Name. It worked for me. But SID is not :( – Shreyas SG Aug 24 '16 at 06:38
  • What exception do you get? Are you sure you are actually using the SID and not a TNS alias? What is the SID you are using, and what is the connection string? Is the SID declared in the listener.ora - what does `lsnrctl status` show? – Alex Poole Aug 24 '16 at 06:42
  • Maybe one of those answers help to find the problem http://stackoverflow.com/questions/29638552/oracle-12c-database-connection-using-thin-driver-throws-io-error/29643222#29643222 or http://stackoverflow.com/questions/35718346/connecting-to-oracle-from-java-exception/35718610#35718610 – SubOptimal Aug 24 '16 at 07:24
  • TNS entries are defined where Oracle is installed on my machine, with just the Service name. Listener.ora is not present, and no where SID is defined. Could that be the reason? – Shreyas SG Aug 24 '16 at 11:04
  • 1
    `listener.ora` is on the database server, not the client. The TNS alias is not necessarily the same as the SID. The listener has to be configured (on the server, by the DBA). Why do you want to use the SID anyway, when service names are more flexible? – Alex Poole Aug 24 '16 at 12:37

1 Answers1

0

addr should be in this way for service Name.

addr = jdbc:oracle:thin:@hostIP:1521/ServiceName

Using the Service Name Syntax:

jdbc:oracle:thin:@//[host]:[tcpPort]/[service_name]

[host]: host name of the database server

[tcpPort]: database listener port

[service_name]: system identifier for the database

Example:

jdbc:oracle:thin:@//myhost:1525/myserviceDB

Please refer below article

https://docs.oracle.com/cd/E12518_01/central_office/pdf/141/html/co_ig/Output/appendix_url.htm

Ravi
  • 159
  • 3
  • 17