2012-08-11 125 views
1
Exception in thread "main" 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 
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:414) 
at oracle.jdbc.driver.T4CConnection.<init>(T4CConnection.java:165) 
at oraenter code herecle.jdbc.driver.T4CDriverExtension.getConnection(T4CDriverExtension.java:35) 
at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:801) 
at java.sql.DriverManager.getConnection(DriverManager.java:582) 
at java.sql.DriverManager.getConnection(DriverManager.java:185) 
at com.test.Date.main(Date.java:13) 

這是我的代碼DB連接

Class.forName("oracle.jdbc.driver.OracleDriver"); 
Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl","system","tiger"); 

我veried tnsnames.ora文件還

ORCL = 
(DESCRIPTION = 
    (ADDRESS = (PROTOCOL = TCP)(HOST = localhost)(PORT = 1521)) 
    (CONNECT_DATA = 
     (SERVER = DEDICATED) 
     (SID = orcl) 
    ) 
) 

我在classpath中添加ojdbc14.jar,我使用oracle10g

你能請幫助我。

+0

可以通過命令提示符'tnsping orcl'查看你正在獲取的內容嗎? – user75ponic 2012-08-11 06:55:38

回答

1

就像提到檢查你是否能夠做一個像tnspingtnsping orcl如果工作正常嘗試連接到sqlplus的來自或類似TOADSQLDeveloper任何工具,你的數據庫。如果您收到任何錯誤,請發佈錯誤消息,否則使用以下命令連接到數據庫並查看它是如何工作的。

try { 
       Class.forName("oracle.jdbc.OracleDriver"); 
      } catch (ClassNotFoundException e) { 
       e.printStackTrace(); 
       return; 
      } 
      Connection con = null; 
      try { 
       con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl","system","tiger"); 
        } catch (SQLException ex) {    
      ex.printStackTrace();    
     } finally { 
      close(connection); 
     } 
return 

Update 1

你可以嘗試用下面的代碼

jdbc:oracle:thin:@//localhost:1521:orcl","system","tiger 

如果不行試試這個,以及

jdbc:oracle:thin:@(DESCRIPTION=(ADDRESS=(PROTOCOL=TCPS)(HOST=<host>)(PORT=<port>))(CONNECT_DATA=(SERVICE_NAME=<service>))) 

更多信息here

Regards

+0

您好,首先非常感謝您的幫助。 – satya 2012-08-12 02:30:58