简体   繁体   English

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

[英]Unable to connect to Oracle 11g on virtual machine via JDBC: Connection reset

I have Windows 7 installed on my laptop which has VMware Workstation with Oracle Linux 5.5. 我在装有VMware Workstation和Oracle Linux 5.5的笔记本电脑上安装了Windows 7。 There is Oracle 11g r2 database on Linux and I need to estabilish the connection via JDBC from host(Win7) to database on guest system(Linux) using NAT. Linux上有Oracle 11g r2数据库,我需要使用NAT建立从主机(Win7)到来宾系统(Linux)上通过JDBC的JDBC连接。 So then I run this java code on Windows: 因此,我在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();
    }
}

I get this exception: 我得到这个例外:

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

On Windows I assigned IPv4-address 172.68.0.1 to VMware Network Adapter VMnet8. 在Windows上,我为VMware Network Adapter VMnet8分配了IPv4地址172.68.0.1。 Here is the picture with my settings for Virtual Network Editor in VMWare and network configurations on Linux: 这是我在VMWare中为Virtual Network Editor设置以及在Linux上的网络配置的图片:

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

On Linux I also have: 在Linux上,我还有:

listener.ora: 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: tnsnames.ora中:

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

So I need to find out what's the problem and how to solve it. 因此,我需要找出问题所在以及如何解决。 Please, help. 请帮忙。

It's late, but I found a solution for this. 已经很晚了,但是我找到了解决方案。

  1. In Oracle VM VirtualBox Manager you have to go to Global Tools then you create a new adapter 在Oracle VM VirtualBox Manager中,您必须转到“全局工具”,然后创建一个新适配器
  2. Automatically it gives an IPv4 Address, with that address you can connect, but first you have to change the setting of your virtual machine, 它会自动提供一个IPv4地址,您可以使用该地址进行连接,但是首先您必须更改虚拟机的设置,
  3. Select your VM, for example Windows 10, then press Settings, now to Network, then in Attached to you have to select Host-only Adapter, in name you select the adapter that you created, then you press Ok. 选择您的VM,例如Windows 10,然后按Settings(设置),现在转到Network(网络),然后在Attached to(附加到)中选择Host-only Adapter(仅主机适配器),在名称中选择您创建的适配器,然后按Ok(确定)。
  4. Run your virtual machine. 运行您的虚拟机。
  5. In Java you can use this: 在Java中,您可以使用以下命令:

    connection.setUrl("jdbc:oracle:thin:@IPv4Address:1521:xe"); connection.setUrl( “JDBC:预言:瘦:@ IPv4Address:1521:XE”);

    connection.setUser("example"); connection.setUser( “例如”);

    connection.setPassword("pass"); connection.setPassword( “通”);

When you complete the all the steps you can connect from your host to the Database in your virtual machine. 完成所有步骤后,您可以从主机连接到虚拟机中的数据库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM