Syed Usama Hashmi
Syed Usama Hashmi

Reputation: 21

Cannot connect to oracle database

I am trying to connect to oracle database 12c using java in eclipse. This is the exception that keeps getting caught.

Exception in thread "main" java.sql.SQLRecoverableException: IO Error: The Network Adapter could not establish the connection
    at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:743)
    at oracle.jdbc.driver.PhysicalConnection.connect(PhysicalConnection.java:666)
    at oracle.jdbc.driver.T4CDriverExtension.getConnection(T4CDriverExtension.java:32)
    at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:566)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at DBHelper.makeConnection(DBHelper.java:31)
    at Main.main(Main.java:37)
Caused by: oracle.net.ns.NetException: The Network Adapter could not establish the connection
    at oracle.net.nt.ConnStrategy.execute(ConnStrategy.java:470)
    at oracle.net.resolver.AddrResolution.resolveAndExecute(AddrResolution.java:506)
    at oracle.net.ns.NSProtocol.establishConnection(NSProtocol.java:595)
    at oracle.net.ns.NSProtocolStream.negotiateConnection(NSProtocolStream.java:246)
    at oracle.net.ns.NSProtocol.connect(NSProtocol.java:264)
    at oracle.jdbc.driver.T4CConnection.connect(T4CConnection.java:1452)
    at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:496)
    ... 7 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:161)
    at oracle.net.nt.ConnOption.connect(ConnOption.java:159)
    at oracle.net.nt.ConnStrategy.execute(ConnStrategy.java:428)
    ... 13 more

Below is what i've done

main.java

import java.sql.Connection;

import java.sql.ResultSet;
import java.sql.SQLException;


public class Main {

  public static void main(String[] arg) throws SQLException{

        DBHelper dbconn = new DBHelper("system", "reborn78");
        Connection conn = dbconn.makeConnection();


        ResultSet result = dbconn.runQuery(conn, "SELECT A,B FROM NEWS");

        System.out.println(result);
  }
}

DBHelper.java

import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;


public class DBHelper {

    private String userName;
    private String passWord;
    private final String url = "jdbc:oracle:thin:@localhost:1521/ORCL"; 

    public DBHelper(String userName, String passWord) throws SQLException{
        Driver myDriver = new oracle.jdbc.driver.OracleDriver();
        DriverManager.registerDriver( myDriver );
        this.userName = userName;
        this.passWord = passWord;     
    }


    public Connection makeConnection() throws SQLException{
          Properties props = new Properties();
          props.setProperty("user", this.userName);
          props.setProperty("password", this.passWord);


          return DriverManager.getConnection(this.url,props);




    }

    public ResultSet runQuery(Connection conn, String sqlQuery) throws SQLException{
        PreparedStatement preStatement;     
        ResultSet result;

            Statement statement = conn.createStatement();
            preStatement = conn.prepareStatement(sqlQuery);
            statement.execute(sqlQuery);
            result = preStatement.executeQuery();
            return result;


    }
}

below is my listener.ora file

# listener.ora Network Configuration File: E:\app\Usama\product\12.1.0\dbhome_1\NETWORK\ADMIN\listener.ora
# Generated by Oracle configuration tools.

SID_LIST_LISTENER =
  (SID_LIST =
    (SID_DESC =
      (SID_NAME = CLRExtProc)
      (ORACLE_HOME = E:\app\Usama\product\12.1.0\dbhome_1)
      (PROGRAM = extproc)
      (ENVS = "EXTPROC_DLLS=ONLY:E:\app\Usama\product\12.1.0\dbhome_1\bin\oraclr12.dll")
    )
  )

LISTENER =
  (DESCRIPTION_LIST =
    (DESCRIPTION =
      (ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC1521))
      (ADDRESS = (PROTOCOL = TCP)(HOST = localhost)(PORT = 1521))
    )
  )

below is my tnsnames.ora file

# tnsnames.ora Network Configuration File: E:\app\Usama\product\12.1.0\dbhome_1\NETWORK\ADMIN\tnsnames.ora
# Generated by Oracle configuration tools.

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

SQL is connected using sqlplus and the listener running and connected to sql developer as well! Below is the link to pictures https://www.dropbox.com/sh/vbv0gkfcz2g59t5/AACtIVR1-bIen99DNN4HDsWaa?dl=0

I have wasted three days getting this right, i was able to get it to work yesterday but today when i restarted my system, the same error is back. I have tried everything available but nothing seems to work today. Thanks in advance

Upvotes: 0

Views: 3753

Answers (1)

Syed Usama Hashmi
Syed Usama Hashmi

Reputation: 21

SOLVED.

I was able to get it to run by re-configuring the database using database configuration assistant and then configuring the net services by net configuration assistant.

Turns out, as I understand, it is the only way to start the services needed to connect via a listener. I have to do it manually and test the net connection in the net configuration assistant every time i restart the system.

Thank you for your support.

Upvotes: 1

Related Questions