无法通过JDBC连接到虚拟机上的Oracle 11g:连接重置

时间:2014-07-20 16:55:39

标签: java linux oracle jdbc vmware

我的笔记本电脑上安装了Windows 7,其中包含带有Oracle Linux 5.5的VMware Workstation。在Linux上有Oracle 11g r2数据库,我需要使用NAT建立从主机(Win7)到客户系统(Linux)上的数据库的JDBC连接。那么我在Windows上运行这个java代码:

public static void main(String[] args) {
    try {
        Class.forName("oracle.jdbc.driver.OracleDriver");
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }
    Connection con;
    try{
        con = DriverManager.getConnection("jdbc:oracle:thin:@172.68.0.1:1521:orcl", "system","oracle");
    } catch (SQLException e) {
        e.printStackTrace();
    }
}

我得到了这个例外:

java.sql.SQLRecoverableException: I/O Exception: Connection reset
    at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:428)
    at oracle.jdbc.driver.PhysicalConnection.<init>(PhysicalConnection.java:536)
    at oracle.jdbc.driver.T4CConnection.<init>(T4CConnection.java:228)
    at oracle.jdbc.driver.T4CDriverExtension.getConnection(T4CDriverExtension.java:32)
    at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:521)
    at java.sql.DriverManager.getConnection(DriverManager.java:571)
    at java.sql.DriverManager.getConnection(DriverManager.java:215)
    at OracleTest.main(OracleTest.java:15)
    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: java.net.SocketException: Connection reset
    at java.net.SocketInputStream.read(SocketInputStream.java:196)
    at java.net.SocketInputStream.read(SocketInputStream.java:122)
    at oracle.net.ns.Packet.receive(Packet.java:283)
    at oracle.net.ns.NSProtocol.connect(NSProtocol.java:287)
    at oracle.jdbc.driver.T4CConnection.connect(T4CConnection.java:1054)
    at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:308)
    ... 12 more

在Windows上,我将IPv4地址172.68.0.1分配给VMware Network Adapter VMnet8。下面是我在VMWare中设置虚拟网络编辑器和Linux上的网络配置的图片:

http://i.cubeupload.com/vAen5q.jpg

在Linux上我也有:

的listener.ora:

LISTENER =
  (DESCRIPTION_LIST =
    (DESCRIPTION =
      (ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC1521))
      (ADDRESS = (PROTOCOL = TCP)(HOST = 172.68.0.1)(PORT = 1521))
    )
  )

ADR_BASE_LISTENER = /u01/app/oracle

tnsnames.ora:

ORCL =    
  (DESCRIPTION =
    (ADDRESS = (PROTOCOL = TCP)(HOST = 172.68.0.1)(PORT = 1521))
    (CONNECT_DATA =
      (SERVER = DEDICATED)
      (SERVICE_NAME = orcl)
    )
  ) 

所以我需要找出问题是什么以及如何解决。请帮忙。

1 个答案:

答案 0 :(得分:0)

已经晚了,但是我找到了解决方案。

  1. 在Oracle VM VirtualBox Manager中,您必须转到“全局工具”,然后创建一个新的适配器
  2. 它会自动提供一个IPv4地址,您可以使用该地址进行连接,但首先必须更改虚拟机的设置,
  3. 选择您的VM,例如Windows 10,然后按Settings(设置),现在转到Network(网络),然后在Attached(附加到)中,选择Host-only Adapter(仅主机适配器),在名称中选择您创建的适配器,然后按OK(确定)。 li>
  4. 运行您的虚拟机。
  5. 在Java中,您可以使用以下代码:

    connection.setUrl(“ jdbc:oracle:thin:@ IPv4Address:1521:xe”);

    connection.setUser(“ example”);

    connection.setPassword(“ pass”);

完成所有步骤后,您可以从主机连接到虚拟机中的数据库。