Oracle数据库的JDBC连接错误

Mer*_*can 1 java oracle url jdbc

我正在尝试建立从Java到Oracle DB的连接.(我的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
}
Run Code Online (Sandbox Code Playgroud)

}

我收到这个错误:

-------- Oracle JDBC Connection Testing ------
Io exception: The Network Adapter could not establish the connection
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么?任何答案将不胜感激..谢谢

Bre*_*ren 8

有三种方法可以写入jdbc url.

如果要连接服务名称,则应在服务名称之前加/

jdbc:oracle:thin:@hostname:port/service_name    --- In your case this is how you need the url
Run Code Online (Sandbox Code Playgroud)

如果你要与sid连接,你应该放在sid之前

jdbc:oracle:thin:@hostname:port:sid
Run Code Online (Sandbox Code Playgroud)

@或@之后使用tns文件中的描述

 jdbc:oracle:thin:@(DESCRIPTION=....)
Run Code Online (Sandbox Code Playgroud)

但不是他们是你的问题的原因.此错误不是SQLException.它是TCP/IP连接异常.这意味着你不知何故无法到达机器.

您是否可以使用其他客户端连接到数据库?我看到你正在使用TOAD.你能与蟾蜍联系吗?您需要确保可以访问服务器.

尝试在命令行上ping到计算机

 ping 85.29.60.47
Run Code Online (Sandbox Code Playgroud)

如果你得到回复,那么尝试端口上的telnet

telnet 85.29.60.47 1521  -- You must have a telnet client installed to do that.
Run Code Online (Sandbox Code Playgroud)

您可能会看到ping或telnet失败.所以这可能是防火墙问题.您需要做的是与网络管理员联系以解决问题.