Oracle

如何在java中連接到oracle伺服器?

  • December 29, 2014

我正在嘗試連接到我的朋友 Oracle 伺服器。但是在連接時我收到錯誤請幫助…..

JDBC 程式碼

Class.forName(oracle.jdbc.driver.OracleDriver).newInstance();
conn=DriverManager.getConnection(jdbc:oracle:thin:@<IPaddress>:<SID>,userName,password);
conn.close();

錯誤資訊

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.wipro.sample.min.main(min.java:28)

   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)
  ... 7 more

   Caused by: java.net.ConnectException: Connection refused: connect
  at java.net.PlainSocketImpl.socketConnect(Native Method)
  at java.net.PlainSocketImpl.doConnect(Unknown Source)
  at java.net.PlainSocketImpl.connectToAddress(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)
  ... 12 more

看起來 oracle 伺服器沒有在聽…

連接到存在 oracle 的機器..

執行su - oracle

lsnrctl 狀態

偵聽器是否處於非活動狀態,然後執行lsnrctl start

您似乎無法連接到 Oracle 伺服器。首先嘗試使用 Oracle 的埠遠端登錄您的 Oracle 伺服器。

前任: telnet <Oracle Serrver IP> 1521

引用自:https://serverfault.com/questions/112199