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

Posted

技术标签:

【中文标题】Oracle JDBC 连接被拒绝(仅来自 jdbc 代码)【英文标题】:Oracle JDBC Connection refused (only from jdbc code) 【发布时间】:2014-08-23 17:51:01 【问题描述】:

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

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

注意: 我正在使用 Ubuntu、IntelliJ IDEA、Tomcat 7、Oracle JDBC 驱动程序 v7.0。

以下独立代码引发相同的异常:

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!");
    

输出:

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

【问题讨论】:

Connection Failed! Check output console 真的没有出现在任何地方的输出中吗?这很奇怪。 @TrippKinetics 它位于最底部。另外,请注意code ticks。它们不用于突出显示文本。 【参考方案1】:

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

explanation here

【讨论】:

已解决。非常感谢,这花了我一整天的时间 :) 添加 @ 对我来说就足够了,不过我不需要最后的斜线。

以上是关于Oracle JDBC 连接被拒绝(仅来自 jdbc 代码)的主要内容,如果未能解决你的问题,请参考以下文章

在休眠中配置时jdbc连接被拒绝

JDBC Java连接被拒绝:连接[重复]

MyEclipse连接Oracle12c测试报java.sql.SQLException: ORA-01017: 用户名/口令无效; 登录被拒绝

数据源拒绝建立连接,来自服务器的消息:“连接太多”

JDBC连接:即使所有Previlleges都被授予,用户也被拒绝访问[关闭]

org.h2.jdbc.JdbcSQLException:连接断开:“java.net.ConnectException:连接被拒绝:连接:本地主机”[90067-193]