[英]JDBC Connection Error to Oracle Database
我正在嘗試建立從Java到Oracle DB的連接。 (我的數據庫在另一台機器上)
我所知道的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,"EXT02501231","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
我究竟做錯了什么? 任何答案將不勝感激..謝謝
有三種寫入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
telnet 85.29.60.47 1521 -- You must have a telnet client installed to do that.
您可能會看到ping或telnet失敗。 因此,這可能是防火牆問題。 您需要做的是就此問題與網絡管理員聯系。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.