2012-12-28 277 views
1

Possible Duplicate:
Java Oracle localhost connection error (ORA-12505)到Oracle 10 XE

當我嘗試連接到Oracle 10g Express Edition的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 
The Connection descriptor used by the client was: 
localhost:1521:xe 

我的示例代碼:

Class.forName("oracle.jdbc.OracleDriver"); 
    Connection connection = null; 
    connection = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe", "mk119", "mk119"); 
    Statement stmt = connection.createStatement(); 

在數據庫:

SQL> connect mk119 
Enter password: 
Connected. 
SQL> ed 
Wrote file afiedt.buf 

    1* select sys_context('userenv', 'instance_name') from dual 
SQL>/

SYS_CONTEXT('USERENV','INSTANCE_NAME') 
---------------------------------------------------------------------- 

xe 

SQL> 

請幫助我。

編輯:堆棧跟蹤:

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:xe 

     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 oracle.jdbc.driver.T4CDriverExtension.getConnection(T4CDriverExtension.java:35) 
     at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:801) 
     at java.sql.DriverManager.getConnection(DriverManager.java:579) 
     at java.sql.DriverManager.getConnection(DriverManager.java:221) 
     at fingerprint.DatabaseTest.main(DatabaseTest.java:14) 

編輯:完整代碼:

import java.sql.*; 

public class DatabaseTest { 
    public static void main(String [] args){ 
     try{ 
      Class.forName("oracle.jdbc.OracleDriver"); 
      Connection connection = null; 
      connection = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe", "mk119", "mk119"); 
      Statement stmt = connection.createStatement(); 
      System.out.println("Connection created"); 
      stmt.close(); 
      connection.close(); 
     }catch(Exception e){ 
      e.printStackTrace(); 
     } 
    } 
} 
+1

看看這個:http://stackoverflow.com/questions/6805705/java-oracle-localhost-connection-error-ora-12505;確保連接的URL是正確的(端口,sid等) – acostache

+0

你可以在DOS提示符下運行'tnsping xe'。如果是這樣,輸出是什麼? – DazzaL

回答

3

按規定here答案,請檢查以下(答案是從相關的一個,可能是重複的,問題):

「檢查listener.ora文件是否爲undefined R上的\ ADMIN \ NETWORK目錄有以下值:

XE = 
    (DESCRIPTION = 
    (ADDRESS = (PROTOCOL = TCP)(HOST = localhost)(PORT = 1521)) 
    (CONNECT_DATA = 
     (SERVER = DEDICATED) 
     (SERVICE_NAME = XE) 
    ) 
) 

因此檢查你的連接網址:主機,端口,SID,以確保它是否設置正確。

+0

是的,我檢查過。 listener.ora沒有這些信息。我補充說,重新啓動數據庫,但仍然無法正常工作。 – UDPLover

+0

您是否收到任何新錯誤? – acostache

+0

不,同樣的錯誤我得到 – UDPLover