2016-05-18 104 views
3

失敗,我有orace11克上192.168.1.217上運行,我試圖使用JDBC驅動程序與Java連接它,它給了我下面的錯誤連接到Oracle 11g從網絡設備通過java

IO Error: The Network Adapter could not establish the connection 

庫我使用是ojdbc6.jar

這裏是我的代碼

public void makeOracleConnection() { 
     try { 
      Class.forName("oracle.jdbc.OracleDriver"); 
      oraCon = DriverManager.getConnection("jdbc:oracle:thin:@192.168.1.217:1521:orcl", "hr", "hr"); 
      oraStmt = oraCon.createStatement(); 
      oraRsStmt=oraCon.createStatement(ResultSet.CONCUR_READ_ONLY,ResultSet.TYPE_SCROLL_INSENSITIVE); 
     } catch (Exception e) { 
      System.out.println("Error while making connection with Database : " + e.getMessage()); 
     } 
    } 

我也試圖在192.168.1.217 ping通則引腳是成功的。 另外TNSLISTENER正在該機器上運行。 請幫忙。

請在這裏找到

run: 
java.sql.SQLRecoverableException: IO Error: The Network Adapter could not establish the connection 

    at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:743) 
    at oracle.jdbc.driver.PhysicalConnection.connect(PhysicalConnection.java:657) 
    at oracle.jdbc.driver.T4CDriverExtension.getConnection(T4CDriverExtension.java:32) 
    at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:560) 
    at java.sql.DriverManager.getConnection(DriverManager.java:664) 
    at java.sql.DriverManager.getConnection(DriverManager.java:247) 
    at test.oracle.makeOracleConnection(oracle.java:30) 
    at test.oracle.<init>(oracle.java:21) 
    at test.oracle.main(oracle.java:69) 
Caused by: oracle.net.ns.NetException: The Network Adapter could not establish the connection 
    at oracle.net.nt.ConnStrategy.execute(ConnStrategy.java:470) 
    at oracle.net.resolver.AddrResolution.resolveAndExecute(AddrResolution.java:506) 
    at oracle.net.ns.NSProtocol.establishConnection(NSProtocol.java:595) 
    at oracle.net.ns.NSProtocol.connect(NSProtocol.java:230) 
    at oracle.jdbc.driver.T4CConnection.connect(T4CConnection.java:1452) 
    at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:496) 
    ... 8 more 
Caused by: java.net.ConnectException: Connection refused: connect 
    at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method) 
    at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:85) 
    at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350) 
    at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206) 
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188) 
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172) 
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) 
    at java.net.Socket.connect(Socket.java:589) 
    at oracle.net.nt.TcpNTAdapter.connect(TcpNTAdapter.java:163) 
    at oracle.net.nt.ConnOption.connect(ConnOption.java:159) 
    at oracle.net.nt.ConnStrategy.execute(ConnStrategy.java:428) 
    ... 13 more 
BUILD SUCCESSFUL (total time: 1 second) 
+0

我們需要更多關於錯誤的信息。如果還記錄了堆棧跟蹤或其他任何內容,則應將其發佈。你還應該確保192.168.1.217中的端口1521實際上正在偵聽(例如,在Linux上運行'ss -l「sport =:1521」')。 –

+0

它在Windows機器上。 –

+0

在已編輯的問題中查找打印堆棧跟蹤 –

回答

3

打印堆棧跟蹤你得到錯誤

java.net.ConnectException: Connection refused: connect 

這意味着沒有什麼監聽的機器和端口你試圖連接到。您的Java代碼看起來是正確的,所以我會繼續調查Oracle實際上正在偵聽192.168.1.217上的端口1521。

如果運行在服務器上運行netstat -n你應該找到一條線,看起來像

TCP [::]:1521    [::]:0     LISTENING 

如果東西真的是,端口上監聽。如果您沒有找到該行,請檢查您的Oracle配置。

+0

它顯示了相同的區別是,LISTENING被TIME_WAIT –

0

嘗試連接其他工具,即sqlplus以驗證問題不在Oracle中。如果你不能用sqlplus/SQL Developer的連接,請確保您的Oracle配置爲允許遠程連接,也聽上給出的地址/端口

+0

取代。並且tnsnames.ora文件在端口1521上有tcp連接,使用sid orcl –

+0

hmm ..好的,那麼也許有一些花哨的防火牆規則阻止了你的連接? – Nadir

+0

我已經禁用了防火牆 –

0
public void makeOracleConnection() { 
    try { 
     Class.forName("oracle.jdbc.OracleDriver"); 
     Connection oraCon = DriverManager.getConnection("jdbc:oracle:thin:@192.168.1.217:1521:orcl", "hr", "hr"); 
     Statement oraStmt = oraCon.createStatement(); 
     //oraRsStmt=oraCon.createStatement(ResultSet.CONCUR_READ_ONLY,ResultSet.TYPE_SCROLL_INSENSITIVE); 
     ResultSet rs = oraStmt.executeQuery("select hello as result from dual"); 

     while(rs.next()) { 
      System.out.println(rs.getString("result")); 
     } 
    } 
    catch (Exception e) 
     System.out.println("Error while making connection with Database : " + e.getMessage()); 
    } 
} 

嘗試了這一點。希望它會有所幫助。我也不喜歡你的連接路徑。這樣對嗎?我認爲它應該是這樣的:

jdbc:oracle:thin:@(DESCRIPTION=(ADDRESS_LIST=(LOAD_BALANCE=YES)(ADDRESS=(PROTOCOL=tcp)(HOST=ip adres)(PORT=port)))(CONNECT_DATA=(SERVICE_NAME = orcl)))","username","password" 
+0

請讓我知道它是否有幫助) –