0

我正在尝试使用 Eclipse IDE 将 oracle 数据库与我的 java 程序连接,我已将 JDBC 驱动程序ojdbc6.jar添加到类路径中。

我正在使用:数据库是:Oracle 11g r1 企业版。操作系统:windows 7 64bit。JDK 版本:1.7.0

我已经在浏览器中使用 url 为“localhost:1158”进行了测试,它打开了 oracle 登录页面,并使用用户名和密码成功登录为“SYSTEM”和“12345”,在我的代码中使用相同,但是在尝试运行 java 程序时一个例外。

我用于测试数据库连接的 java 程序是:

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

public class Connect 
{
    public static void main(String[] args) {
        try {
            Class.forName("oracle.jdbc.OracleDriver");
            Connection con = DriverManager.getConnection("jdbc:oracle:thin:@//localhost:1158:v$instance","SYSTEM","12345");
            System.out.println("connected.."+con);
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

}

例外情况是

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 abhishek.Connect.main(Connect.java:12)
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:206)
    at oracle.jdbc.driver.T4CConnection.connect(T4CConnection.java:966)
    at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:292)
    ... 7 more
Caused by: java.net.UnknownHostException: //localhost
    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

谁能告诉我我做错了什么以及异常的含义。

4

1 回答 1

0

端口1158用于 DbConsole 接口,不适用于 JDBC。

JDBC 应连接到的 Oracle 侦听器的默认端口是 port 1521

于 2013-02-17T19:52:27.813 回答