我试图建立从Java到Oracle DB的连接。 (我的DB在另一台机器上)
我知道的URL形式如下:String url =" jdbc:oracle:thin:@hostname:portnumber:sid&#34 ;;
这是我建立连接的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
我做错了什么?任何答案将不胜感激.. 感谢
答案 0 :(得分:7)
有三种方法可以写入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失败。所以这可能是防火墙问题。您需要做的是就此问题与网络管理员联系。