网络适配器无法建立连接 - Oracle 11g
Posted
技术标签:
【中文标题】网络适配器无法建立连接 - Oracle 11g【英文标题】:The network adapter could not establish the connection - Oracle 11g 【发布时间】:2013-08-03 21:20:08 【问题描述】:我编写了一个 servlet 程序,我希望它连接到我的 Oracle 数据库,但它给了我一个错误。我正在使用 Eclipse Helios、Tomcat 6.0.37、Oracle 11g (11.2.0.1.0)、ojdbc6.jar(在服务器项目的运行配置中添加)。我可以通过 SQL Plus 接口连接到数据库,甚至可以从 Eclipse 的数据库开发角度开始连接。但是当我试图通过我的 servlet 连接时,我得到了错误。对于我的 servlet,我使用端口号 9090,而对于数据库,我使用默认端口号 1521。
java.sql.SQLException: The Network Adapter could not establish the connection
at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:412)
at oracle.jdbc.driver.PhysicalConnection.<init>(PhysicalConnection.java:531)
at oracle.jdbc.driver.T4CConnection.<init>(T4CConnection.java:221)
at oracle.jdbc.driver.T4CDriverExtension.getConnection(T4CDriverExtension.java:32)
at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:503)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at com.android.collegenotificationsystem.RegistrationServlet.doGet(RegistrationServlet.java:43)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:723)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:861)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:606)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Unknown Source)
Caused by: oracle.net.ns.NetException: The Network Adapter could not establish the connection
at oracle.net.nt.ConnStrategy.execute(ConnStrategy.java:359)
at oracle.net.resolver.AddrResolution.resolveAndExecute(AddrResolution.java:422)
at oracle.net.ns.NSProtocol.establishConnection(NSProtocol.java:672)
at oracle.net.ns.NSProtocol.connect(NSProtocol.java:237)
at oracle.jdbc.driver.T4CConnection.connect(T4CConnection.java:1042)
at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:301)
... 21 more
Caused by: java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.waitForConnect(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 oracle.net.nt.TcpNTAdapter.connect(TcpNTAdapter.java:141)
at oracle.net.nt.ConnOption.connect(ConnOption.java:123)
at oracle.net.nt.ConnStrategy.execute(ConnStrategy.java:337)
... 26 more
这是我建立连接的代码:
Class.forName("oracle.jdbc.OracleDriver");
Connection con = (Connection) DriverManager.getConnection( "jdbc:oracle:thin:@localhost:1521:orcl","username","password");
我尝试执行 lsnrctl stat 命令,结果如下:
LSNRCTL for 32-bit Windows: Version 11.2.0.1.0 - Production on 03-AUG-2013 16:08
:46
Copyright (c) 1991, 2010, Oracle. All rights reserved.
Connecting to (DESCRIPTION=(ADDRESS=(PROTOCOL=IPC)(KEY=EXTPROC1521)))
STATUS of the LISTENER
------------------------
Alias LISTENER
Version TNSLSNR for 32-bit Windows: Version 11.2.0.1.0 - Produ
ction
Start Date 03-AUG-2013 12:14:19
Uptime 0 days 3 hr. 54 min. 28 sec
Trace Level off
Security ON: Local OS Authentication
SNMP OFF
Listener Parameter File C:\app\Owner\product\11.2.0\dbhome_1\network\admin\lis
tener.ora
Listener Log File c:\app\owner\diag\tnslsnr\Owner-PC\listener\alert\log.
xml
Listening Endpoints Summary...
(DESCRIPTION=(ADDRESS=(PROTOCOL=ipc)(PIPENAME=\\.\pipe\EXTPROC1521ipc)))
(DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=127.0.0.1)(PORT=1521)))
Services Summary...
Service "CLRExtProc" has 1 instance(s).
Instance "CLRExtProc", status UNKNOWN, has 1 handler(s) for this service...
Service "orcl.168.1.56" has 1 instance(s).
Instance "orcl", status READY, has 1 handler(s) for this service...
Service "orclXDB.168.1.56" has 1 instance(s).
Instance "orcl", status READY, has 1 handler(s) for this service...
The command completed successfully
这些是我的 tnsnames.ora 文件的内容:
LISTENER_ORCL =
(ADDRESS = (PROTOCOL = TCP)(HOST = localhost)(PORT = 1521))
ORACLR_CONNECTION_DATA =
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC1521))
)
(CONNECT_DATA =
(SID = CLRExtProc)
(PRESENTATION = RO)
)
)
ORCL =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = localhost)(PORT = 1521))
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = orcl.168.1.56)
)
)
任何帮助将不胜感激。提前致谢。
【问题讨论】:
你能告诉我们你在eclipse中用来连接这个数据库实例的连接信息吗?连接参数的屏幕截图肯定会有所帮助 我要问一个显而易见的问题:Servlet 是否与 Oracle DB 在同一台机器上运行? 如果它是基于云的,那么检查数据库 ID。 【参考方案1】:我有类似的问题。它通过一个简单的命令为我解决了。
lsnrctl start
导致网络适配器异常的原因是:
-
数据库主机名或端口号错误 (OR)
数据库 TNSListener 尚未启动。 TNSListener 可以使用
lsnrctl
实用程序启动。
尝试使用命令提示符启动监听器:
-
点击开始,在搜索栏中输入
cmd
,当cmd
出现在选项列表中时,右键点击它并选择“以管理员身份运行”。
在命令提示符窗口中,键入不带引号的lsnrctl start
,然后按 Enter。
输入 Exit
并按 Enter。
希望对你有帮助。
【讨论】:
你看到 Ash 发布的lsnrctl status
的输出了吗?
在 macOS 上怎么样?我收到lsnrctl: command not found
【参考方案2】:
首先检查您的监听器是打开还是关闭。 转到网络管理器,然后本地 -> 服务命名 -> orcl。 然后更改您的主机名并输入您的 PC 名称。 现在转到 LISTENER 并更改 HOST 并输入您的 PC 名称。
【讨论】:
我几乎搜索了整个互联网!但这解决了我的问题。 +1 顺便问一下,hostname
必须是 PC 名称吗?还是只是一个约定或什么?
主机名应该不同于 PC 名称。以上是关于网络适配器无法建立连接 - Oracle 11g的主要内容,如果未能解决你的问题,请参考以下文章
我在我的 oracle 数据库中收到错误“网络适配器无法建立连接”