JDBC connection to Oracle: listener used the connection with the following error: ora-12505

Exception in thread "main" 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:419)
	at oracle.jdbc.driver.PhysicalConnection.<init>(PhysicalConnection.java:536)
	at oracle.jdbc.driver.T4CConnection.<init>(T4CConnection.java:228)
	at oracle.jdbc.driver.T4CDriverExtension.getConnection(T4CDriverExtension.java:32)
	at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:521)
	at java.sql.DriverManager.getConnection(DriverManager.java:571)
	at java.sql.DriverManager.getConnection(DriverManager.java:215)
	at com.test.OracleTest.main(OracleTest.java:13)
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 connect descriptor
 
	at oracle.net.ns.NSProtocol.connect(NSProtocol.java:386)
	at oracle.jdbc.driver.T4CConnection.connect(T4CConnection.java:1054)
	at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:308)
	... 7 more

//create a Connection object.
Connection conn = DriverManager getConnection (” JDBC: oracle: thin: @ localhost: 1521: former “, “admit”, “123”);
The problem is the database to monitor the sid in the configuration file is not correct, namely the “former” in the url, enter the database installation directory, find tnsnames. Ora file, for example, my path is/home/Oracle_11g/product/11.2.0/dbhome_1/network/admin/tnsnames. Ora
found SERVICE_NAME = XE, but not equal to the former, So you can change the url of the former to XE can
Connection conn = DriverManager. GetConnection (” JDBC: oracle: thin: @ localhost: 1521: former “, “admit”, “123”);

Read More: