ORA-12505, TNS:listener does not currently know of

2019-01-01 14:54发布

I have installed Oracle 11g Express Edition Release 2 in my windows 7 64 bit OS and tried to execute JDBC program, then I got the following error:

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:412)
    at oracle.jdbc.driver.PhysicalConnection.<init>(PhysicalConnection.java:531)
    at oracle.jdbc.driver.T4CConnection.<init>(T4CConnection.java:221)
    at oracle.jdbc.driver.T4CDriverExtension.getConnection(T4CDriverExtension.java:32)
    at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:503)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at com.jlcindia.jdbc.JDBCUtil.geOracleConnection(JDBCUtil.java:28)
    at Lab3O.main(Lab3O.java:15)
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:385)
    at oracle.jdbc.driver.T4CConnection.connect(T4CConnection.java:1042)
    at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:301)
    ... 8 more

标签: oracle jdbc
23条回答
余欢
2楼-- · 2019-01-01 15:07

There are a few things that can cause this problem, but before you get started with JDBC, you need to be sure that you can connect to the database using SQL*Plus. If you're not familiar with SQL*Plus, it's a command-line tool for connecting to Oracle databases that has been a standard part of Oracle for a long time and it is included with Oracle XE.

When connecting to an Oracle database using JDBC, you don't connect to the database directly. Instead, you connect to a TNS listener, which then connects you to the database. The error ORA-12505 means that the listener was up and you could connect to it, but it couldn't connect you to the database because it doesn't know that that database is up. There are two reasons for this:

  • the database has not been started up,
  • the database has not registered with the listener, e.g. because the database was started before the listener. (When the database starts, it registers itself with a listener if it is already running. If the listener isn't running, the database doesn't register itself, and if the listener starts, it doesn't go looking for databases that might register with it.)

ORA-12505 means that the listener knows about that database, but the listener hasn't received a notification from the database that the database is up. (If you were trying to connect to the wrong database, using the wrong SID, you would get an ORA-12154 error "TNS: could not resolve the connect identifier specified".)

What Oracle services are running in the Services snap-in? (Open this from Control Panel > Administrative Tools > Services, or just Start > Run > services.msc.) You need the services OracleServiceXE and OracleXETNSListener to be running.

If the services have both been started, can you connect to the database in SQL*Plus using any of the following at a command prompt? (I'm assuming you're running these on the machine you've installed Oracle XE on.)

sqlplus system/system-password@XE
sqlplus system/system-password
sqlplus / as sysdba

(Replace system-password with the password you set for the SYS and SYSTEM users during the Oracle XE installation.)

The first of these three connect via the TNS listener, but the second two connect directly to the database without going via the listener, and only work if you're on the same machine as the database. If the first one fails but the other two succeed, then JDBC connections will also fail. If so, connect to the database using either of the other two and run ALTER SYSTEM REGISTER. Then exit from SQL*Plus and try the first form again.

If the third one fails but the second one works, add your user account to the ora_dba group. Do this in Control Panel > Computer Management > Local Users and Groups.

Once you can get connections of the form

sqlplus system/system-password@XE

to work, you ought to be able to connect to Oracle XE via JDBC. (Incidentally, you haven't shown us the JDBC code you're using to connect to the database, but I would suspect that it is quite probably correct; there would be various other errors if parts of the connection string were wrong.)

查看更多
呛了眼睛熬了心
3楼-- · 2019-01-01 15:10

Check by doing tnsping and instance name in host machine. It will give u the tns decription and all most of the time host name is different which is not matching.

I resolve my issue likewise

In Unix machine $ tnsping (Enter)

It gives me full tns description where I found that host name is different.. :)

查看更多
春风洒进眼中
4楼-- · 2019-01-01 15:12

Faced similar error, any of the above solutions didn't help. There was a problem in the listner.ora file. By mistake I had added SID out of the SID_LIST see below(section between the stars *).

 SID_LIST_LISTENER =
      (SID_LIST =

        (SID_DESC =
          (SID_NAME = PLSExtProc)
          (ORACLE_HOME = C:\oraclexe\app\oracle\product\11.2.0\server)
          (PROGRAM = extproc)
        )
        (SID_DESC =
          (SID_NAME = CLRExtProc)
          (ORACLE_HOME = C:\oraclexe\app\oracle\product\11.2.0\server)
          (PROGRAM = extproc)
        )
        )

 *(SID_DESC =
           (SID_NAME = XE)
           (ORACLE_HOME = C:\oraclexe\app\oracle\product\11.2.0\server)
         )*
    LISTENER =
      (DESCRIPTION_LIST =
        (DESCRIPTION =
          (ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC1))
          (ADDRESS = (PROTOCOL = TCP)(HOST = 127.0.0.1)(PORT = 1521))
        )
      )


    DEFAULT_SERVICE_LISTENER = (XE)

Corrected this error as below:

SID_LIST_LISTENER =
  (SID_LIST =
    (SID_DESC =
       (SID_NAME = XE)
       (ORACLE_HOME = C:\oraclexe\app\oracle\product\11.2.0\server)
     )
    (SID_DESC =
      (SID_NAME = PLSExtProc)
      (ORACLE_HOME = C:\oraclexe\app\oracle\product\11.2.0\server)
      (PROGRAM = extproc)
    )
    (SID_DESC =
      (SID_NAME = CLRExtProc)
      (ORACLE_HOME = C:\oraclexe\app\oracle\product\11.2.0\server)
      (PROGRAM = extproc)
    )
    )
LISTENER =
  (DESCRIPTION_LIST =
    (DESCRIPTION =
      (ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC1))
      (ADDRESS = (PROTOCOL = TCP)(HOST = 127.0.0.1)(PORT = 1521))
    )
  )


DEFAULT_SERVICE_LISTENER = (XE)

Stopped and the database enter image description here

Stopped the listeners OracleServiceXE and OracleXETNSListener manually as it did not stop automatically by going to Control Panel\All Control Panel Items\Administrative Tools\Services. Restarted the database and it worked like a charm.

查看更多
余欢
5楼-- · 2019-01-01 15:12

Got the same issue all over sudden after messing with some cmd's. Turned out the ORACLE SERVICE XE had stopped running. Took me 2 secs to restart it from the administrative tools. tnx!

查看更多
姐姐魅力值爆表
6楼-- · 2019-01-01 15:13

In my case not was working out, finally i restarted my oracle and TNS listener and everything worked. Was struggling for 2 days.

查看更多
听够珍惜
7楼-- · 2019-01-01 15:15

When your getting this error "ORA-12505, TNS:listener does not currently know of SID given in connect descriptor"

Solution: Open Services, and start OracleServiceXE, after that try to connect...

查看更多
登录 后发表回答