Java Oracle localhost connection error (ORA-12505)

此生再无相见时 提交于 2019-11-28 02:07:37

Check if listener.ora file under the <ORACLE_HOME>\admin\NETWORK directory has the following value:

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

Instead of String url = "jdbc:oracle:thin:@" + serverName + ":" + portNumber + ":" + sid;

use this:

String url = "jdbc:oracle:thin:@" + serverName + ":" + portNumber + "/" + sid;

replace xe with database name which you have set during installation, you will surely get success

if you forgot the dbname it could be retreived from file tnsnames.ora in your oracle directory

user3877876

I faced same problem while connecting to oracle rac. I changed the url from port:servicename to port/servicename and it worked for me.

I'm going to guess that the TNS listener has started, but the database instance started up before the listener did.

When the database instance starts up, it will register itself with the TNS listener. However, if there's no listener to register with, it can't do this. When the listener starts up, it doesn't check to see whether the instances it knows about have started up.

I can provide a demonstration. I'm using Oracle 11g XE Beta on Windows 7. Initially, the OracleServiceXE service is running but OracleXETNSListener service is not.

I ran your database connection code and I got the following error:

Exception in thread "main" java.sql.SQLRecoverableException: IO Error: The Network Adapter could not establish the connection

If you're getting a ORA-12505 error, then clearly your TNS listener is running.

I then started the TNS listener and re-ran your database connection code. I got the following output this time: (I've renamed your class and changed the username and password within it, but other than that, the code within it is the same):

C:\Users\Luke\stuff>java DbConnTest
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
[stacktrace snipped]

(This error isn't identical to yours: I didn't get a The Connection descriptor used by the client was: section in it. I'm not 100% sure why.)

In the case above, the fix is connect to SQL*Plus as SYS and run ALTER SYSTEM REGISTER. This registers the instance with the listener:

C:\Users\Luke\stuff>sqlplus / as sysdba

SQL*Plus: Release 11.2.0.2.0 Beta on Sun Jul 24 11:13:57 2011

Copyright (c) 1982, 2010, Oracle.  All rights reserved.


Connected to:
Oracle Database 11g Express Edition Release 11.2.0.2.0 - Beta

SQL> alter system register;

System altered.

SQL> exit
Disconnected from Oracle Database 11g Express Edition Release 11.2.0.2.0 - Beta

After doing this, I was able to connect to the database:

C:\Users\Luke\stuff>java DbConnTest
Success
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!