Unable to get oracle database connection

kusumat picture kusumat · Mar 11, 2017 · Viewed 10k times · Source

1.JAVA_HOME is set to IBM JAVA SDK. 2.Unable to get oracle database connection. Where as tried running the same on machine having Oracle java set as JAVA_HOME 3.Placed ojdbc14.jar under AppServer/lib.

   try {

  connection = DriverManager.getConnection(
      "jdbc:oracle:thin:@[server-ip]:[server-port]/[dbname]", "[uname]", "[pass]");

} catch (SQLException e) {

  System.out.println("Connection Failed! Check output console");
  e.printStackTrace();
  return;

}

if (connection != null) {
  System.out.println("You made it, take control your database now!");
} else {
  System.out.println("Failed to make connection!");
}

Is there any setting i need to do.Below is the error

Oracle JDBC Driver Registered! Connection Failed! Check output console java.sql.SQLRecoverableException: IO Error: The Network Adapter could not establish the connection at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:517) at oracle.jdbc.driver.PhysicalConnection.(PhysicalConnection.java:557) at oracle.jdbc.driver.T4CConnection.(T4CConnection.java:233) at oracle.jdbc.driver.T4CDriverExtension.getConnection(T4CDriverExtension.java:29) at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:556) at java.sql.DriverManager.getConnection(DriverManager.java:583) at java.sql.DriverManager.getConnection(DriverManager.java:227) at OracleJDBCExample.main(OracleJDBCExample.java:29) Caused by: oracle.net.ns.NetException: The Network Adapter could not establish the connection at oracle.net.nt.ConnStrategy.execute(ConnStrategy.java:389) at oracle.net.resolver.AddrResolution.resolveAndExecute(AddrResolution.java:431) at oracle.net.ns.NSProtocol.establishConnection(NSProtocol.java:882) at oracle.net.ns.NSProtocol.connect(NSProtocol.java:267) at oracle.jdbc.driver.T4CConnection.connect(T4CConnection.java:1625) at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:365) ... 7 more Caused by: java.net.SocketTimeoutException: connect timed out at java.net.PlainSocketImpl.socketConnect(Native Method) at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:370) at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:231) at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:213) at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:404) at java.net.Socket.connect(Socket.java:643) at oracle.net.nt.TcpNTAdapter.connect(TcpNTAdapter.java:147) at oracle.net.nt.ConnOption.connect(ConnOption.java:130) at oracle.net.nt.ConnStrategy.execute(ConnStrategy.java:367)

Thanks, Kusuma

Answer

Frank Schmitt picture Frank Schmitt · Mar 12, 2017

Here's a couple of things to check:

  • can you connect to port [server-port] on [server-ip] ? (try telnet, netcat or something similar)
  • does the listener accept connections? i.e. does sqlplus username/pw@[server-ip]:[server-port]/[dbname] work?
  • is the SID correct (dbname) ?
  • have you tried connecting with the service name instead of the SID: jdbc:oracle:thin:@//<server-ip>:<server-port>/<dbname> ?

see also Java JDBC - How to connect to Oracle using Service Name instead of SID