JDBC - Cant connect to Oracle using Service Name

6.9k Views Asked by At

I have been trying to connect to this Oracle database with JDBC thin driver with the following syntax:

var URL = "jdbc:oracle:thin:@//16.161.286.56:1522/Service_Name"; 
var USER = "user";
var PASS = "password";
var conn = Jdbc.getConnection(URL, USER, PASS);

I keep on having the same response when I execute this:

We're sorry, a server error occurred. Please wait a bit and try again. [87a99af]

Would anyone have an idea of what I am doing wrong ?


Also, when I change the IP address to its 'string version':

var URL = "jdbc:oracle:thin:@//mydomain.com:1522/Service_Name";

Then I get the error response:

Failed to establish a database connection. Check connection string, username and password.

Which does not make sense to me as both expressions are supposed to be equivalent ...

3

There are 3 best solutions below

0
On

Use this:

jdbc:oracle:thin:@//hostname:1521/ServiceName

3
On

The actual format of Oracle JDBC connection using service name is:

@//host_name:port_number/service_name

Make sure that is service name is ok. You can try with TNSNameListener

jdbc:oracle:thin:@(description=(address=(host=<HOSTADDRESS>)(protocol=tcp)(port=<PORT>))(connect_data=(service_name=<SERVICENAME>)(server=<SHARED>)))

The TNSNameListener file location:

<ORACLE_HOME>\network\admin\tnsnames.ora
example: /home/oracle/oracle/product/10.2.0/db_1/network/admin/tnsnames.ora
0
On

I am not sure which class is that Jdbc is. Take a look at the JDBCUrlSample.java and DataSourceSample.java