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

当前回答

我通过在TNSNAMES中将“SID”更改为“SERVICE_NAME”来修复这个问题。ora文件。

请查看您的DB是否要求SID或SERVICE_NAME。

干杯

其他回答

如果你使用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>`

当你浏览start->run->services.msc时,请检查OracleServiceXE和OracleXETNSListener的状态是否已启动。

对于我的情况下,只有OracleXETNSListener启动,但OracleServiceXE没有启动,当我开始右键单击->启动并检查连接它为我工作

我也得到了同样的错误,但当他们尝试所有三个失败。 如果以上三点都失败了。尝试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

这应该可以工作…

我找到了这个异常的一些原因。他们是

1)默认为数据库XE的名称。因此url将是“jdbc:oracle:thin:@localhost:1521:XE”。

2)确保OracleServiceXE,OracleXETNSListener正在运行。它将在控制面板\所有控制面板项\管理工具\服务

我也遇到过同样的问题,通过重新启动OracleServiceXE服务解决了这个问题。转到服务。然后验证“OracleServiceXE”服务是否正常运行