2015-06-16 56 views
2

我正在使用Oracle 12C作爲我的數據庫。 我已經用Java編寫了一個小型的JDBC連接程序,但是我面臨着一個監聽器的問題。ORA-12505:TNS監聽器當前不知道連接描述符中給出的SID

import java.sql.Connection; 
import java.sql.DriverManager; 
import java.sql.PreparedStatement; 
import java.sql.ResultSet; 
import java.sql.SQLException; 

public class JdbcConnection { 
    public static void main(String[] args) throws SQLException,ClassNotFoundException { 
     String url = "jdbc:oracle:thin:@localhost:1521:orcl"; 
     String user = "system"; 
     String password = "password"; 
     Connection connection = null; 

     Class.forName("oracle.jdbc.driver.OracleDriver"); 
     connection = DriverManager.getConnection(url, user, password); 
     if(connection!=null){ 
      System.out.println("Success in connnection"); 
     } else { 
      System.out.println("failure in connection "); 
     } 
    } 
} 

我得到以下異常:

C:\Users\Administrator\Desktop>java JdbcConnection 
Exception in thread "main" java.sql.SQLException: Listener refused the connectio 
n with the following error: 
ORA-12505, TNS:listener does not currently know of SID given in connect descriptor 
The Connection descriptor used by the client was: 
localhost:1521:orcl 

     at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java 
:112) 
     at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java 
:261) 
     at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:387) 
     at oracle.jdbc.driver.PhysicalConnection.<init>(PhysicalConnection.java: 
441) 
     at oracle.jdbc.driver.T4CConnection.<init>(T4CConnection.java:165) 
     at oracle.jdbc.driver.T4CDriverExtension.getConnection(T4CDriverExtensio 
n.java:35) 
     at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:801) 
     at java.sql.DriverManager.getConnection(Unknown Source) 
     at java.sql.DriverManager.getConnection(Unknown Source) 
     at JdbcConnection.main(JdbcConnection.java:18) 

這是lsnrctl status

LSNRCTL for 64-bit Windows: Version 12.1.0.1.0 - Production on 16-JUN-2015 13:43 
:41 

Copyright (c) 1991, 2013, Oracle. All rights reserved. 

Welcome to LSNRCTL, type "help" for information. 

LSNRCTL> status 
Connecting to (DESCRIPTION=(ADDRESS=(PROTOCOL=IPC)(KEY=EXTPROC1521))) 
STATUS of the LISTENER 
------------------------ 
Alias      LISTENER 
Version     TNSLSNR for 64-bit Windows: Version 12.1.0.1.0 - Produ 
ction 
Start Date    16-JUN-2015 12:02:52 
Uptime     0 days 1 hr. 40 min. 52 sec 
Trace Level    off 
Security     ON: Local OS Authentication 
SNMP      OFF 
Listener Parameter File C:\app\orauser\product\12.1.0\dbhome_1\network\admin\l 
istener.ora 
Listener Log File   C:\app\orauser\diag\tnslsnr\hydwemvm\listener\alert\lo 
g.xml 
Listening Endpoints Summary... 
    (DESCRIPTION=(ADDRESS=(PROTOCOL=ipc)(PIPENAME=\\.\pipe\EXTPROC1521ipc))) 
    (DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=hydwemvm)(PORT=1521))) 
Services Summary... 
Service "CLRExtProc" has 1 instance(s). 
    Instance "CLRExtProc", status UNKNOWN, has 1 handler(s) for this service... 
The command completed successfully 
+0

你能告訴我們代碼嗎? – Kryptos

+0

您是否爲計算機上的Oracle和偵聽器啓動了Windows服務? –

+0

是的,服務正在運行,我也重新啓動了服務。 –

回答

10

如果你知道你的Oracle數據庫SID輸出,然後用

jdbc:oracle:thin:@localhost:1521:orcl 

otherwi參見下面的使用情況下,你有服務名稱

jdbc:oracle:thin:@localhost:1521/orcl 

此外,確保服務名稱與名稱ORCL應該是啓動和運行。如果仍然無法工作,那麼您需要重新啓動機器,然後重試。

還在,無法正常工作?然後,嘗試以下操作:

登錄與用戶SYSTEM通過以下SQLS運行註冊LOCAL_LISTENER

alter system set local_listener = '(ADDRESS=(PROTOCOL=TCP)(HOST=localhost)(PORT=1521))' scope = both; 
alter system register; 

如何檢查Oracle SID和服務名稱:

SELECT sys_context('USERENV', 'SID') FROM DUAL; -- It will return your oracle database SID 

SELECT sys_context('USERENV', 'SERVICE_NAME') FROM DUAL; -- It will return your oracle database service name 
+0

我已經嘗試了上述兩種格式仍然沒有運氣 –

+0

然後,如我所說,重新啓動您的機器,並嘗試。 – Ravi

1

如果你想知道你的數據庫的默認SID在sqlplus使用此查詢:

SELECT sys_context('USERENV', 'SID') FROM DUAL; 

在JDBC URL中使用此值而不是「orcl」。

1

你可以使用下面的URL嗎?
注意不同之處,這是使用SERVICENAME而不是SID。

jdbc:oracle:thin:@localhost:1521/orclservice 
相關問題