3

I have orace 11g running on 192.168.1.217 and I am trying to connect it using JDBC driver with java and it gives me following error

IO Error: The Network Adapter could not establish the connection

Library I am using is ojdbc6.jar

Here is my code

public void makeOracleConnection() {
        try {
            Class.forName("oracle.jdbc.OracleDriver");
            oraCon = DriverManager.getConnection("jdbc:oracle:thin:@192.168.1.217:1521:orcl", "hr", "hr");
            oraStmt = oraCon.createStatement();
            oraRsStmt=oraCon.createStatement(ResultSet.CONCUR_READ_ONLY,ResultSet.TYPE_SCROLL_INSENSITIVE);
        } catch (Exception e) {
            System.out.println("Error while making connection with Database  : " + e.getMessage());
        }
    }

I have also tried to ping on 192.168.1.217 then pins is successful. Also TNSLISTENER is running on that machine. please help.

Please find print stack trace here

run:
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:657)
    at oracle.jdbc.driver.T4CDriverExtension.getConnection(T4CDriverExtension.java:32)
    at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:560)
    at java.sql.DriverManager.getConnection(DriverManager.java:664)
    at java.sql.DriverManager.getConnection(DriverManager.java:247)
    at test.oracle.makeOracleConnection(oracle.java:30)
    at test.oracle.<init>(oracle.java:21)
    at test.oracle.main(oracle.java:69)
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.NSProtocol.connect(NSProtocol.java:230)
    at oracle.jdbc.driver.T4CConnection.connect(T4CConnection.java:1452)
    at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:496)
    ... 8 more
Caused by: java.net.ConnectException: Connection refused: connect
    at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method)
    at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:85)
    at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
    at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
    at java.net.Socket.connect(Socket.java:589)
    at oracle.net.nt.TcpNTAdapter.connect(TcpNTAdapter.java:163)
    at oracle.net.nt.ConnOption.connect(ConnOption.java:159)
    at oracle.net.nt.ConnStrategy.execute(ConnStrategy.java:428)
    ... 13 more
BUILD SUCCESSFUL (total time: 1 second)
Mr x
  • 828
  • 1
  • 8
  • 25
  • We need more information about the error. If a stack trace, or anything else, is logged as well you should post it. You should also ensure that something is actually listening on port 1521 in 192.168.1.217 (e.g. run `ss -l "sport = :1521"` on Linux). – K Erlandsson May 18 '16 at 06:14
  • it is on windows machine. – Mr x May 18 '16 at 06:20
  • Find print stack trace in edited question – Mr x May 18 '16 at 06:22
  • Maybe stupid, but did the user *hr* get the right to connect? We had the issue several times that an oracle account got created for us by operations, but they forgot to grant him the right to connect. Have a look here http://stackoverflow.com/questions/9447492/how-to-create-a-user-in-oracle-11g-and-grant-permissions – cheffe May 20 '16 at 06:18
  • I am able to get connect with hr using sqldeveloper but when I try with remote machine it does not works – Mr x May 20 '16 at 15:01

3 Answers3

3

You get the error

java.net.ConnectException: Connection refused: connect

Which means that there is nothing listening on the machine and port you are trying to connect to. Your Java code looks correct so I would continue to investigate that Oracle is actually listening on port 1521 on 192.168.1.217.

If you run run netstat -n on the server you should find a line that looks like

TCP    [::]:1521              [::]:0                 LISTENING

If something really is listening on that port. If you do not find that line, check your Oracle configuration.

K Erlandsson
  • 13,408
  • 6
  • 51
  • 67
0

Try to connect with some other tool, ie sqlplus to verify that the issue is not with Oracle. If you cannot connect with sqlplus/sql developer, make sure that your oracle is configured to allow remote connections, and also listens on given addresses/ports

Nadir
  • 1,369
  • 1
  • 15
  • 28
  • sqlplus works fine. And tnsnames.ora file has tcp connection on port 1521 with sid orcl – Mr x May 18 '16 at 07:58
  • hmm.. ok, so maybe some fancy firewall rules that block your connection? – Nadir May 18 '16 at 09:23
  • i have disabled firewall – Mr x May 18 '16 at 11:26
  • ok, and when you connect via sqlplus, you use hr account or some built in? Maybe the db is in mounted state (but not started) and you need to log in with dba credentials and start it correctly (just a lucky guess, I'm running out of ideas) – Nadir May 18 '16 at 11:37
0
public void makeOracleConnection() {
    try {
        Class.forName("oracle.jdbc.OracleDriver");
        Connection oraCon = DriverManager.getConnection("jdbc:oracle:thin:@192.168.1.217:1521:orcl", "hr", "hr");
        Statement oraStmt = oraCon.createStatement();
        //oraRsStmt=oraCon.createStatement(ResultSet.CONCUR_READ_ONLY,ResultSet.TYPE_SCROLL_INSENSITIVE);
        ResultSet rs = oraStmt.executeQuery("select hello as result from dual");

        while(rs.next()) {
             System.out.println(rs.getString("result"));
        } 
    } 
    catch (Exception e)
        System.out.println("Error while making connection with Database  : " + e.getMessage());
    }
}

Try this out. Hope it'll help. I also don't like your connection path. Is it right? I think it should be something like this:

jdbc:oracle:thin:@(DESCRIPTION=(ADDRESS_LIST=(LOAD_BALANCE=YES)(ADDRESS=(PROTOCOL=tcp)(HOST=ip adres)(PORT=port)))(CONNECT_DATA=(SERVICE_NAME = orcl)))","username","password"
Medet Koilybay
  • 111
  • 2
  • 9