Home >Java >javaTutorial >How to Connect to an Oracle Database Using a Service Name in Java?

How to Connect to an Oracle Database Using a Service Name in Java?

Linda Hamilton
Linda HamiltonOriginal
2024-12-03 14:43:10629browse

How to Connect to an Oracle Database Using a Service Name in Java?

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn