Oracle JDBC 连接被拒绝(仅来自 jdbc 代码)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24539579/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 02:24:22  来源:igfitidea点击:

Oracle JDBC Connection refused (only from jdbc code)

oraclejdbcconnectexception

提问by Vituel

I'm getting Connection refusedwhen I try to connect to a remote Oracle database.

我收到Connection refused的时候我尝试连接到远程Oracle数据库。

I can successfully telnet to the same IP address and port and I can even connect via SQL Developer or IntelliJ Idea's Databasetab using the same credentials, but when I try it from my code I get the exception.

我可以成功地 telnet 到相同的 IP 地址和端口,我什至可以Database使用相同的凭据通过 SQL Developer 或 IntelliJ Idea 的选项卡进行连接,但是当我从我的代码中尝试它时,我得到了异常。

Note: I'm Using Ubuntu, IntelliJ IDEA, Tomcat 7, Oracle JDBC Driver v7.0.

注意:我使用的是 Ubuntu、IntelliJ IDEA、Tomcat 7、Oracle JDBC Driver v7.0。

The following standalone code throws the same exception:

以下独立代码抛出相同的异常:

public static void main(String[] argv) {

    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;
    }

    System.out.println("Oracle JDBC Driver Registered!");

    Connection connection = null;

    try {
        connection = DriverManager.getConnection(
                "jdbc:oracle:thin:10.1.1.27:1521:xe", "user", "password");
    } catch (SQLException e) {
        System.out.println("Connection Failed! Check output console");
        e.printStackTrace();
        return;
    }

    if (connection != null) {
        System.out.println("You made it, take control your database now!");
    } else {
        System.out.println("Failed to make connection!");
    }
}

Output:

输出:

Oracle JDBC Driver Registered!

java.sql.SQLRecoverableException: IO Error: The Network Adapter could not establish the connection
    at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:673)
    at oracle.jdbc.driver.PhysicalConnection.<init>(PhysicalConnection.java:715)
    at oracle.jdbc.driver.T4CConnection.<init>(T4CConnection.java:385)
    at oracle.jdbc.driver.T4CDriverExtension.getConnection(T4CDriverExtension.java:30)
    at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:564)
    at java.sql.DriverManager.getConnection(DriverManager.java:571)
    at java.sql.DriverManager.getConnection(DriverManager.java:215)
    at com.greenmile.web.DBConnectionTest.main(DBConnectionTest.java:34)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)

Caused by: oracle.net.ns.NetException: The Network Adapter could not establish the connection
    at oracle.net.nt.ConnStrategy.execute(ConnStrategy.java:445)
    at oracle.net.resolver.AddrResolution.resolveAndExecute(AddrResolution.java:464)
    at oracle.net.ns.NSProtocol.establishConnection(NSProtocol.java:594)
    at oracle.net.ns.NSProtocol.connect(NSProtocol.java:229)
    at oracle.jdbc.driver.T4CConnection.connect(T4CConnection.java:1360)
    at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:486)
    ... 12 more

Caused by: java.net.ConnectException: Connection refused
    at java.net.PlainSocketImpl.socketConnect(Native Method)
    at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339)
    at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
    at java.net.Socket.connect(Socket.java:579)
    at oracle.net.nt.TcpNTAdapter.connect(TcpNTAdapter.java:162)
    at oracle.net.nt.ConnOption.connect(ConnOption.java:133)
    at oracle.net.nt.ConnStrategy.execute(ConnStrategy.java:411)
    ... 17 more

Connection Failed! Check output console

回答by Bjarte Brandt

Replace you jdbc connect string "jdbc:oracle:thin:10.1.1.27:1521:xe"with "jdbc:oracle:thin:@10.1.1.27:1521/xe"

将您的 jdbc 连接字符串替换"jdbc:oracle:thin:10.1.1.27:1521:xe""jdbc:oracle:thin:@10.1.1.27:1521/xe"

explanation here

解释在这里