2014-09-02 276 views
1

我試圖建立從Java到Oracle DB的連接。 (我的數據庫在另一臺機器上)JDBC連接錯誤到Oracle數據庫

我所知道的URL的形式如下所示:String url =「jdbc:oracle:thin:@hostname:portnumber:sid」;

這裏是我的Java代碼建立連接:

package net.metric.action; 

import java.io.IOException; 
import java.io.PrintWriter; 
import java.sql.Connection; 
import java.sql.DriverManager; 
import java.sql.ResultSet; 
import java.sql.Statement; 

import javax.servlet.ServletException; 
import javax.servlet.http.HttpServlet; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 

public class DemoServlet extends HttpServlet { 
    private static final long serialVersionUID = 1L; 
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 

    response.setContentType("text"); 
    PrintWriter out = response.getWriter(); 

    System.out.println("-------- Oracle JDBC Connection Testing ------"); 

    try { 

     Class.forName("oracle.jdbc.driver.OracleDriver"); 

    } catch (ClassNotFoundException e) { 

     System.out.println("Where is your Oracle JDBC Driver?"); 
     e.printStackTrace(); 
     return; 

    } 
    try{ 
      DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver()); 
      //CONNECT TO DB 
      String url = "jdbc:oracle:thin:@252.112.60.47:1521:XE"; 
      System.out.println(url); 

      Connection conn = DriverManager.getConnection(url,"EXT025","Tellcom30"); 
       conn.setAutoCommit(false); 
       Statement stmt = conn.createStatement(); 
       System.out.println("OK"); 

       /* ResultSet rset = 
        stmt.executeQuery("select * from SBO_AUDIT_NEW.AUDIT_EVENT"); 
       while (rset.next()) { 
        System.out.println (rset.getString(1)); 
       } 
       stmt.close(); 
       System.out.println ("Ok.");*/ 


    }catch(Exception e){ 
     System.out.println(e.getMessage()); 
    } 

} 

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
    // TODO Auto-generated method stub 
} 

}

我得到這個錯誤:

-------- Oracle JDBC Connection Testing ------ 
Io exception: The Network Adapter could not establish the connection 

我在做什麼錯?任何答案將不勝感激。 謝謝

+0

你確認你的oracle數據庫設置爲接受本地主機之外的連接嗎? – abalos 2014-09-02 12:15:41

+0

我希望您的示例中的用戶名和密碼是假的:-) – Leo 2014-09-02 12:16:10

+0

ping您遠程主機,並檢查它是否接受遠程連接,以及爲什麼您需要註冊驅動程序兩次? – SparkOn 2014-09-02 12:16:29

回答

5

有三種方式寫入jdbc url。

如果您正在使用的服務名稱連接,你應該把/前服務名稱

jdbc:oracle:thin:@hostname:port/service_name --- In your case this is how you need the url 

如果您正在使用SID連接,你應該把:SID之前

jdbc:oracle:thin:@hostname:port:sid 

或使用說明你的tns文件後@

jdbc:oracle:thin:@(DESCRIPTION=....) 

但是非他們是你的問題的原因。此錯誤不是SQLException。這是一個TCP/IP連接異常。這意味着你以某種方式無法到達機器。

你能否用另一個客戶端連接到數據庫?我看到你正在使用TOAD。你能夠連接蟾蜍嗎?您需要確保您可以訪問服務器。

嘗試ping本機命令行

ping 85.29.60.47 

,如果你得到響應回來然後嘗試口

telnet 85.29.60.47 1521 -- You must have a telnet client installed to do that. 

你可能會看到ping或遠程登錄失敗的遠程登錄。所以這可能是一個防火牆問題。您需要做的是與網絡管理員聯繫,然後解決問題。

+0

我無法從我的機器和蟾蜍ping 85.29.60.47在我有Oracle DB的機器上沒有執行,我想這是關於防火牆的問題,正如你提到的。非常感謝@gomyes你的回答對我來說很清楚:) – Mertcan 2014-09-02 12:41:00