我的笔记本电脑上安装了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)
)
)
所以我需要找出问题是什么以及如何解决。请帮忙。
答案 0 :(得分:0)
已经晚了,但是我找到了解决方案。
在Java中,您可以使用以下代码:
connection.setUrl(“ jdbc:oracle:thin:@ IPv4Address:1521:xe”);
connection.setUser(“ example”);
connection.setPassword(“ pass”);
完成所有步骤后,您可以从主机连接到虚拟机中的数据库。