oracle tnsname

This is caused by the requested SID doesn’t exist in {ORACLE_HOME}/network/admin/tnsnames.ora

P.S Tested with Oracle database 19c with ojdbc8.jar

1. JDBC


	try (Connection conn = DriverManager.getConnection(
                "jdbc:oracle:thin:@localhost:1521:xe", "system", "password")) {

           //...
		   
	} catch (SQLException e) {
		e.printStackTrace();
	} catch (Exception e) {
		e.printStackTrace();
	}

Output:


SQL State: 66000
Listener refused the connection with the following error:
ORA-12505, TNS:listener does not currently know of SID given in connect descriptor

The SID xe doesn’t exist in ORACLE_HOME/network/admin/tnsnames.ora

2. tnsnames.ora

Sample.

C:\{ORACLE_HOME}\NETWORK\ADMIN\tnsnames.ora

# tnsnames.ora Network Configuration File: C:\{ORACLE_HOME}\NETWORK\ADMIN\tnsnames.ora
# Generated by Oracle configuration tools.

LISTENER_ORCL =
  (ADDRESS = (PROTOCOL = TCP)(HOST = localhost)(PORT = 1521))


ORACLR_CONNECTION_DATA =
  (DESCRIPTION =
    (ADDRESS_LIST =
      (ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC1521))
    )
    (CONNECT_DATA =
      (SID = CLRExtProc)
      (PRESENTATION = RO)
    )
  )

ORCL =
  (DESCRIPTION =
    (ADDRESS = (PROTOCOL = TCP)(HOST = localhost)(PORT = 1521))
    (CONNECT_DATA =
      (SERVER = DEDICATED)
      (SERVICE_NAME = orcl)
    )
  )

In above tnsnames.ora sample, the SID is ORCL

To fix it, update the code:


	try (Connection conn = DriverManager.getConnection(
		"jdbc:oracle:thin:@localhost:1521:orcl", "system", "password")) {

		//...
		
	} catch (SQLException e) {
		e.printStackTrace();
	} catch (Exception e) {
		e.printStackTrace();
	}

References