Connection refused to OracleDB 12c in docker from JDBC

Solution for Connection refused to OracleDB 12c in docker from JDBC
is Given Below:

Hi so I’m learning JDBC and was trying to connect to an Oracle DB 12c in a docker container. I am able to access the database from SQLDeveloper with the same credentials but unable to do so from the Java application. I’m not sure what is wrong, I have checked the login credentials and they seem to be correct. The port 1521 is also the port I used from sqlplus and SQLDeveloper. Would really appreciate any help!

My application

import java.sql.*;

public class JDBCEX1 {
    public static void main(String[] args) {
        String url = "jdbc:oracle:thin:@localhost:1521:xe";
        String userName = "System";
        String password = "oracle";
        Connection con = null;
        String query = "SELECT * from Employee";
        Statement st = null;
        try {
            // step1: loading driver
            Class.forName("oracle.jdbc.OracleDriver");
            // step 2: creating connection
            con = DriverManager.getConnection(url, userName, password);
            // step 3: create query and execute
            st = con.createStatement();
            ResultSet rs = st.executeQuery(query);
            // step 4
            while (rs.next()) {
                int id = rs.getInt("ID");
                String name = rs.getString("NAME");
                int age = rs.getInt("AGE");

                System.out.println("Id " + id);
                System.out.println("Name " + name);
                System.out.println("age " + age);
            }
            // step 5:
            con.close();

        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

Errors

java.sql.SQLRecoverableException: IO Error: The Network Adapter could not establish the connection
    at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:673)
    at oracle.jdbc.driver.PhysicalConnection.<init>(PhysicalConnection.java:715)
    at oracle.jdbc.driver.T4CConnection.<init>(T4CConnection.java:385)
    at oracle.jdbc.driver.T4CDriverExtension.getConnection(T4CDriverExtension.java:30)
    at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:564)
    at java.sql.DriverManager.getConnection(DriverManager.java:664)
    at java.sql.DriverManager.getConnection(DriverManager.java:247)
    at JDBCEX1.main(JDBCEX1.java:18)
Caused by: oracle.net.ns.NetException: The Network Adapter could not establish the connection
    at oracle.net.nt.ConnStrategy.execute(ConnStrategy.java:445)
    at oracle.net.resolver.AddrResolution.resolveAndExecute(AddrResolution.java:464)
    at oracle.net.ns.NSProtocol.establishConnection(NSProtocol.java:594)
    at oracle.net.ns.NSProtocol.connect(NSProtocol.java:229)
    at oracle.jdbc.driver.T4CConnection.connect(T4CConnection.java:1360)
    at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:486)
    ... 7 more
Caused by: java.net.ConnectException: Connection refused (Connection refused)
    at java.net.PlainSocketImpl.socketConnect(Native Method)
    at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:476)
    at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:218)
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:200)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:394)
    at java.net.Socket.connect(Socket.java:606)
    at oracle.net.nt.TcpNTAdapter.connect(TcpNTAdapter.java:162)
    at oracle.net.nt.ConnOption.connect(ConnOption.java:133)
    at oracle.net.nt.ConnStrategy.execute(ConnStrategy.java:411)
    ... 12 more

P.S. I decided to do this through docker because I’m using a Mac and couldn’t install oracle db 12c any other way. If anyone has any tips, would appreciate it as well.