Connecting to Oracle Using Service Name in Java
In Java, connecting to an Oracle database typically involves specifying the hostname, port, and Oracle SID. However, some Oracle databases use a "Service Name" instead of SID. To connect to such a database, a slight modification in the JDBC URL is required.
The correct syntax for connecting to Oracle using a Service Name is:
jdbc:oracle:thin:@//host_name:port_number/service_name
For example, if the hostname is "oracle.example.com", the port number is 1522, and the Service Name is "ABCD," the JDBC URL would be:
jdbc:oracle:thin:@//oracle.example.com:1522/ABCD
In case the JDBC URL includes a TNS name, the syntax would be:
jdbc:oracle:thin:@(DESCRIPTION =(ADDRESS_LIST =(ADDRESS =(PROTOCOL=TCP)(HOST=host_name)(PORT=port_number)))(CONNECT_DATA=(SERVICE_NAME=<service_name>)))
For instance, if the TNS name is "BlahSID," the JDBC URL would be:
jdbc:oracle:thin:@(DESCRIPTION =(ADDRESS_LIST =(ADDRESS =(PROTOCOL=TCP)(HOST=blah.example.com)(PORT=1521)))(CONNECT_DATA=(SERVICE_NAME=BlahSID)))
The above is the detailed content of How to Connect to an Oracle Database Using a Service Name in Java?. For more information, please follow other related articles on the PHP Chinese website!