我已经在我的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

当前回答

我的甲骨文停止工作,我得到这个错误。我重启了我的机器,也尝试了上述解决方案。最终,我打开了组件服务并重新启动了oracle服务,一切都开始工作了。希望这能帮助到一些人。

其他回答

如果你使用Oracle Express Edition,你应该有这个url

jdbc:oracle:thin:@localhost:1521:xe或jdbc:oracle:thin:@localhost:1521/ xe

我在pom.xml中的liquibase配置插件中遇到了类似的问题。我改变了我的配置:

`<configuration>
   <driver>oracle.jdbc.OracleDriver</driver>
   <url>jdbc:oracle:thin:@localhost:1521:xe</url>
   <defaultSchemaName></defaultSchemaName>
   <username>****</username>
   <password>****</password>
</configuration>`

我也得到了同样的错误,但当他们尝试所有三个失败。 如果以上三点都失败了。尝试LSNRCTL状态,如果您发现服务(在我的情况下是XE)丢失尝试这个

sqlplus /nolog
conn  system
alter system register;  
exit  
lsnrctl status  

现在您可以看到服务 即使没看到也试试这个

sqlplus /nolog  
conn system  
alter system set local_listener = '(ADDRESS=(PROTOCOL=TCP)(HOST=localhost)(PORT=1521))' scope = both;  
alter system register;  
exit  
lsnrctl status

这应该可以工作…

在我的情况下没有工作,最后我重新启动我的oracle和TNS监听器和一切工作。挣扎了2天。

连接con = DriverManager.getConnection (" jdbc: oracle:薄:@localhost: 1521: xe”,“斯科特”、“老虎”);

我得到的错误:

java.sql.SQLException:监听器拒绝连接,错误如下: ORA-12505, TNS:监听器当前不知道连接描述符中给定的SID 客户端使用的连接描述符是: localhost: 1521: xe

我是怎么解决的:

连接con = DriverManager.getConnection (" jdbc: oracle:薄:localhost: 1521: xe”,“斯科特”、“老虎”);

(删除@)

不知道为什么,但它现在工作了…

@Luke Woodward的回答帮助我确定我的XE没有连接。 我在连接VPN时遇到了一个问题。我使用的是Windows 11上的最新本地Oracle 21c。 文件监听器。Ora和tnsnames。我们使用我的电脑名称“ERIK-PC”是这样的:

LISTENER =
  (DESCRIPTION_LIST =
    (DESCRIPTION =
      (ADDRESS = (PROTOCOL = TCP)(HOST = ERIK-PC)(PORT = 1521))
      (ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC1521))
    )
  )

我只需要将ERIK-PC更改为localhost。 更多信息在这个帖子里。 顺便说一下,我使用的是Oracle 21c,从18c开始,*。ora文件在installationfolder\homes\OraDB21Home1\network\admin\*.ora下 保存您的更改并使用特权的windows shell重新启动侦听器:

lsnrctl stop
lsnrctl start