Home >Java >javaTutorial >How to Correctly Connect to an Oracle Database Using Service Name in JDBC?
Connecting to Oracle with Service Name vs. SID in JDBC
One of the common requirements in Java applications using JDBC is connecting to an Oracle database. In the past, Oracle SID was commonly used for this purpose, but many modern databases now leverage Oracle Service Name instead. This article addresses the issue of connecting to Oracle using Service Name rather than SID in a Java application.
In the provided scenario, the JDBC URL attempts to connect to a database using Oracle Service Name, but it fails. The main challenge is the incorrect syntax for connecting using Service Name.
The correct syntax for connecting to Oracle using Service Name in JDBC is:
jdbc:oracle:thin:@//host_name:port_number/service_name
Therefore, the corrected URL would be:
jdbc:oracle:thin:@//oracle.hostserver2.mydomain.ca:1522/ABCD
Alternatively, the Oracle TNS name can also be specified in the JDBC URL, as follows:
jdbc:oracle:thin:@(DESCRIPTION =(ADDRESS_LIST =(ADDRESS =(PROTOCOL=TCP)(HOST=blah.example.com)(PORT=1521)))(CONNECT_DATA=(SID=BLAHSID)(GLOBAL_NAME=BLAHSID.WORLD)(SERVER=DEDICATED)))
By using the correct syntax, the JDBC application can successfully connect to the Oracle database using Service Name.
The above is the detailed content of How to Correctly Connect to an Oracle Database Using Service Name in JDBC?. For more information, please follow other related articles on the PHP Chinese website!