Listener refused the connection with the following error: ORA-12505, TNS:listener does not currently

Error using Java connection oACLE12C:
java.sql.SQLException: Listener refused the connection with the following error:
ORA-12505, TNS:listener does not currently know of SID given in connect descriptor
at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:673)
at oracle.jdbc.driver.PhysicalConnection.< init> (PhysicalConnection.java:711)
at oracle.jdbc.driver.T4CConnection.< init> (T4CConnection.java:385)
at oracle.jdbc.driver.T4CDriverExtension.getConnection(T4CDriverExtension.java:30)
at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:558)
at java.sql.DriverManager.getConnection(DriverManager.java:664)
at java.sql.DriverManager.getConnection(DriverManager.java:247)
at demo1.DBHelp.getCon(DBHelp.java:16)
at demo1.DBHelp.main(DBHelp.java:28)
Caused by: oracle.net.ns.NetException: Listener refused the connection with the following error:
ORA-12505, TNS: listener does not currently know of SID given in the connect descriptor

the at oracle.net.ns.NSProtocolStream.negotiateConnection (NSProtocolStream. Java: 272)
the at oracle.net.ns.NSProtocol.connect(NSProtocol.java:263)
at oracle.jdbc.driver.T4CConnection.connect(T4CConnection.java:1360)
at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:486)
. 8 more
obviously, this is because the listener cannot recognize the database SID in the connection identifier, go to the network to look up
The solution online is as follows: find the tnsnames.ora file and open it using Notepad or another tool. Note that this is the path of the ORACLE 12C file and different versions of Oracle are in different paths
D:\oracle\app\Administrator\product\12.1.0\dbhome_1\NETWORK\ADMIN tnsnames.ora

# tnsnames.ora NETWORK Configuration File: D:\oracle\app\Administrator\product\12.1.0\dbhome_1\network\admin\tnsnames. Ora
# Generated by oracle configuration tools.

LISTENER_TEST =
(ADDRESS = (PROTOCOL = TCP)(HOST = Localhost)(PORT = 1521))

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

ORACLR_CONNECTION_DATA =
(DESCRIPTION =
(ADDRESS_LIST) =
(ADDRESS = (PROTOCOL = IPC) (KEY = EXTPROC1521))

(CONNECT_DATA =

(SID = CLRExtProc) (the PRESENTATION = RO)

ORACLE12C =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = localhost)(PORT = 1521)))
(CONNECT_DATA =
(SERVER = oracle12c))
(SERVICE_NAME = oracle12c)
)
)
// Change the red part to the one shown above. The green part is the instance name of the database when you installed it.

D: Oracle \ App \Administrator\ Product \12.1.0\dbhome_1\NETWORK\ADMIN Listener. Ora file
The contents of the file are as follows:
# listener.ora Network Configuration File: D:\oracle\app\Administrator\product\12.1.0\dbhome_1\network\admin\listener. Ora
# Generated by oracle configuration tools.
SID_LIST_LISTENER =
(SID_LIST =
(SID_DESC =
(SID_NAME = CLRExtProc)
(ORACLE_HOME = D:\oracle\app\Administrator\product\12.1.0\dbhome_1)
(PROGRAM = extproc)
(ENVS = “EXTPROC_DLLS = ONLY: D: \ oracle \ app \ Administrator \ product \ 12.1.0 \ dbhome_1 \ bin \ oraclr12 DLL”)
)

 (SID_DESC =
(GLOBAL_DBNAME = oracle12c)
(ORACLE_HOME = D:\oracle\app\Administrator\product\12.1.0\dbhome_1)
(SID_NAME = oracle12c)
)

)
LISTENER =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = localhost)(PORT = 1521))
(ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC1521))
)

The red part is to add, save, and then restart the Oracle service.
The content of the test is shown below
package demo1;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class DBHelp {
private static String URL = “JDBC :oracle:thin:@127.0.0.1:1521:oracle12”;
private static String USERNAME = “system”;
private static String PASSWORD = “940109”;
public static Connection getCon(){
Connection conn = null;
try {
Class.forName(“oracle.jdbc.driver.OracleDriver”);
conn = DriverManager.getConnection(URL,USERNAME,PASSWORD);
if(conn! =null)
System.out.println(“connect successful”);
} catch (Exception e) {
e.printStackTrace();
}
return conn;
}
/**
* @param args
*/
public static void main(String[] args) {
System.out.println(new DBHelp().getCon());
}
public static void close(Connection con,Statement sm,ResultSet rs){
try {
if(con! =null){
con.close();
}
if(sm! =null){
sm.close();
}
if(rs ! = null){
rs.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
// There will be no more errors
connect successful
oracle.jdbc.driver.T4CConnection@1edf1c96
In other words, when installing Oracle12c, I changed the name of the instance by myself. If it is installed according to the default instance, then the above content basically does not need to be modified by myself.
Is not installed as the default instance.

Read More: