java.sql.SQLException:网络适配器无法建立连接

时间:2016-06-26 16:36:50

标签: java sql-server oracle

我今天面临着一个问题。我试图用我的数据库进行震动,这是在我的电脑上安装的Oracle 11g,并使用JAR Ojdbc6,使用几行java代码连接它。但是我所尝试的任何事情都没有奏效。 试过: - 使用SQL DEV =>连接 - Start Listener =>一切准备就绪 - 尝试从URL更改Ip =>仍然无法连接 我看到这是一个普通错误,但没有人有一个很好的回应。我无法理解为什么SQL DEV正在运行,但Eclipse没有。

我希望有人可以'帮助我,

这是我的代码:

package testare;

import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.SQLException;

public class oracle {

    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:@localhost:1521/orcl", "hr","hr");
        } 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 Connection Testing ------
Oracle JDBC Driver Registered!
Connection Failed! Check output console
java.sql.SQLException: The Network Adapter could not establish the connection
    at oracle.jdbc.driver.SQLStateMapping.newSQLException(SQLStateMapping.java:70)
    at oracle.jdbc.driver.DatabaseError.newSQLException(DatabaseError.java:133)
    at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:199)
    at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:480)
    at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:413)
    at oracle.jdbc.driver.PhysicalConnection.<init>(PhysicalConnection.java:508)
    at oracle.jdbc.driver.T4CConnection.<init>(T4CConnection.java:203)
    at oracle.jdbc.driver.T4CDriverExtension.getConnection(T4CDriverExtension.java:33)
    at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:510)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at testare.oracle.main(oracle.java:31)
Caused by: oracle.net.ns.NetException: The Network Adapter could not establish the connection
    at oracle.net.nt.ConnStrategy.execute(ConnStrategy.java:328)
    at oracle.net.resolver.AddrResolution.resolveAndExecute(AddrResolution.java:421)
    at oracle.net.ns.NSProtocol.establishConnection(NSProtocol.java:630)
    at oracle.net.ns.NSProtocol.connect(NSProtocol.java:329)
    at oracle.jdbc.driver.T4CConnection.connect(T4CConnection.java:966)
    at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:292)
    ... 7 more
Caused by: java.net.ConnectException: Connection refused: connect
    at java.net.DualStackPlainSocketImpl.connect0(Native Method)
    at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
    at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
    at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
    at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
    at java.net.PlainSocketImpl.connect(Unknown Source)
    at java.net.SocksSocketImpl.connect(Unknown Source)
    at java.net.Socket.connect(Unknown Source)
    at java.net.Socket.connect(Unknown Source)
    at java.net.Socket.<init>(Unknown Source)
    at java.net.Socket.<init>(Unknown Source)
    at oracle.net.nt.TcpNTAdapter.connect(TcpNTAdapter.java:127)
    at oracle.net.nt.ConnOption.connect(ConnOption.java:126)
    at oracle.net.nt.ConnStrategy.execute(ConnStrategy.java:306)
    ... 12 more

1 个答案:

答案 0 :(得分:1)

我认为正确的网址是jdbc:oracle:thin:@localhost:1521:orcl