'Establishing JDBC through SID

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.



Solution 1:[1]

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ravi