我已经在我的windows 7 64位操作系统中安装了Oracle 11g Express Edition Release 2,并试图执行JDBC程序,然后我得到了以下错误:

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

当前回答

我在SQL Workbench中也遇到过类似的问题。

URL:

jdbc: oracle:薄:@111.111.111.111:1111:xe

是行不通的。

URL:

jdbc: oracle:薄:@111.111.111.111:1111:asdb

的工作原理。

这对我的具体情况有帮助。恐怕还有许多其他的原因和不同的解决办法。

其他回答

我刚来的时候也有同样的问题。我刚刚在Windows 8(64位)上安装了Oracle 12c,但我已经在命令行上通过'TNSPING xe'解决了它…如果连接未建立或名称未找到,请尝试数据库名称,在我的情况下,它是'orcl'…再次使用' tnspring orcl',如果它ping成功,那么你需要在这种情况下将SID更改为'orcl'(或任何你使用的数据库名称)…

有一些事情会导致这个问题,但是在开始使用JDBC之前,您需要确保可以使用SQL*Plus连接到数据库。如果你不熟悉SQL*Plus,它是一个命令行工具,用于连接Oracle数据库,很长一段时间以来一直是Oracle的标准部分,它包含在Oracle XE中。

使用JDBC连接到Oracle数据库时,不直接连接到数据库。相反,您连接到一个TNS侦听器,然后它将您连接到数据库。ORA-12505错误意味着侦听器已经启动,您可以连接到它,但它不能将您连接到数据库,因为它不知道数据库已经启动。原因有二:

数据库尚未启动, 数据库没有注册到监听器,例如,因为数据库在监听器之前启动。(当数据库启动时,如果它已经在运行,它会向侦听器注册自己。如果监听器没有运行,数据库就不会注册自己,如果监听器启动了,它也不会去寻找可能注册到它的数据库。)

ORA-12505意味着侦听器知道该数据库,但是侦听器还没有收到来自数据库的数据库启动的通知。(如果你试图连接到错误的数据库,使用错误的SID,你会得到一个ORA-12154错误“TNS:无法解析指定的连接标识符”)

服务管理单元中正在运行哪些Oracle服务?(从控制面板>管理工具>服务打开,或只需启动>运行> Services .msc。)您需要运行OracleServiceXE和OracleXETNSListener服务。

如果两个服务都已启动,您是否可以在命令提示符下使用以下任何一种方法连接到SQL*Plus中的数据库?(我假设您正在安装Oracle XE的机器上运行这些程序。)

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

(“SYSTEM -password”替换为安装Oracle XE时设置的“SYS”和“SYSTEM”用户密码。)

这三种方法中的第一个通过TNS侦听器连接,但后两个不通过侦听器直接连接到数据库,并且只有当您与数据库在同一台机器上时才能工作。如果第一个失败,但其他两个成功,那么JDBC连接也将失败。如果是,使用其他两个中的一个连接到数据库,并运行ALTER SYSTEM REGISTER。然后退出SQL*Plus并再次尝试第一个表单。

如果第三个失败而第二个成功,则将您的用户帐户添加到ora_dba组中。在控制面板>计算机管理>本地用户和组中执行此操作。

一旦你可以得到连接的形式

sqlplus system/system-password@XE

为了工作,您应该能够通过JDBC连接到Oracle XE。(顺便提一下,您还没有向我们展示用于连接数据库的JDBC代码,但我怀疑它很可能是正确的;如果连接字符串的某些部分出错,就会出现各种其他错误。)

I too faced the same issue. I had installed Oracle Express edition 10g in Windows XP OS using VMware and it was working fine. Since it was very awkward typing SQL queries in the SQL utility provided by 10g and since I was used to working with SQL developer, I installed 32 bit SQL developer in XP and tried connecting to my DB SID "XE". But the connection failed with error-ORA-12505 TNS listener doesn't currently know of SID given in connect descriptor. I was at sea as to how this problem occurred since it was working fine with the SQL utility and I had also created few Informatica mappings using the same. I did browse a lot on this stuff hither thither and applied the suggestions offered to me after pinging the status of "lsnrctl" on public forums but to no avail. However, this morning I tried creating a new connection again, and Voila, it worked with no issues. I am guessing after reading in few posts that sometimes listener listens before the DB connects or something(pardon me for my crude reference as I am a newbie here) but I suggest to just restart the machine and check again.

我通过纠正JDBC代码解决了这个问题。

正确的JDBC字符串应该是…

conection = DriverManager.getConnection
("jdbc:oracle:thin:@localhost:1521:xe","system","ishantyagi");

但是我使用的JDBC字符串是…

conection = DriverManager.getConnection
("jdbc:oracle:thin:@localhost:1521:orcl","system","ishantyagi");

因此,指定orcl而不是xe的错误显示了这个错误,因为SID名称是错误的。

我在SQL Workbench中也遇到过类似的问题。

URL:

jdbc: oracle:薄:@111.111.111.111:1111:xe

是行不通的。

URL:

jdbc: oracle:薄:@111.111.111.111:1111:asdb

的工作原理。

这对我的具体情况有帮助。恐怕还有许多其他的原因和不同的解决办法。