3

I am attempting to connect to an Oracle database through Java with the Oracle JDBC driver with the following code (obscuring the host, service, user, and password):

import java.sql.*;

public class Main {
    public Main () {
        try {
            String host = "HOST_NAME";
            String port = "1521";
            String service = "SERVICE_NAME";
            String user = "SCHEMA_USER";
            String password = "SCHEMA_PASSWORD";

            Class.forName("oracle.jdbc.driver.OracleDriver");

            Connection connection = DriverManager.getConnection("jdbc:oracle:thin:@(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=" + 
                        host + 
                        ")(PORT=" + 
                        port + 
                        ")))(CONNECT_DATA=(SERVICE_NAME=" + 
                        service + 
                        ")))", 
                        user, 
                        password);

            connection.close ();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main (String args) {
        new Main ();
    }
}

However, I receive the following error:

java.sql.SQLException: IO Error: The Network Adapter could not establish the connection
    at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:458)
    at oracle.jdbc.driver.PhysicalConnection.<init>(PhysicalConnection.java:546)
    at oracle.jdbc.driver.T4CConnection.<init>(T4CConnection.java:236)
    at oracle.jdbc.driver.T4CDriverExtension.getConnection(T4CDriverExtension.java:32)
    at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:521)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at com.acxiom.axle.reporting.database.DatabaseConnection.connect(DatabaseConnection.java:23)
    at com.acxiom.axle.reporting.Reporting.establishDatabaseConnection(Reporting.java:53)
    at com.acxiom.axle.reporting.Reporting.beginReporting(Reporting.java:20)
    at com.acxiom.axle.reporting.Entry.<init>(Entry.java:28)
    at com.acxiom.axle.reporting.Entry.main(Entry.java:118)
Caused by: oracle.net.ns.NetException: The Network Adapter could not establish the connection
    at oracle.net.nt.ConnStrategy.execute(ConnStrategy.java:392)
    at oracle.net.resolver.AddrResolution.resolveAndExecute(AddrResolution.java:434)
    at oracle.net.ns.NSProtocol.establishConnection(NSProtocol.java:687)
    at oracle.net.ns.NSProtocol.connect(NSProtocol.java:247)
    at oracle.jdbc.driver.T4CConnection.connect(T4CConnection.java:1102)
    at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:320)
    ... 11 more
Caused by: java.net.UnknownHostException: null
    at java.net.Inet6AddressImpl.lookupAllHostAddr(Native Method)
    at java.net.InetAddress$2.lookupAllHostAddr(Unknown Source)
    at java.net.InetAddress.getAddressesFromNameService(Unknown Source)
    at java.net.InetAddress.getAllByName0(Unknown Source)
    at java.net.InetAddress.getAllByName(Unknown Source)
    at java.net.InetAddress.getAllByName(Unknown Source)
    at oracle.net.nt.TcpNTAdapter.connect(TcpNTAdapter.java:117)
    at oracle.net.nt.ConnOption.connect(ConnOption.java:133)
    at oracle.net.nt.ConnStrategy.execute(ConnStrategy.java:370)
    ... 16 more

The strange thing is, I can connect to the database from PL/SQL Developer, I can ping the remote host, and I can telnet to the remote host on port 1521.

Why would only Java appear to give an UnknownHostException, but I can connect and ping the host with other applications?

EDIT: I removed "hr/hr" from the connection string above. I have tried the connection as-is with it removed and still receive the same error. I've also tried changing the connection string to match the version morgano listed in his answer, with the same result. Finally, I tried to change the port number to a port I know it's not listening on, and it still receives the same error.

Alex
  • 557
  • 1
  • 7
  • 15
  • read here http://stackoverflow.com/questions/12574414/io-error-the-network-adapter-could-not-establish-the-connection – PeterMmm Mar 24 '15 at 18:55
  • @PeterMmm I'm pretty sure I've ruled out the three causes listed by EJP in that question. The database is most certainly running, as I can connect to it via PL/SQL Developer and telnet, which also rules out the firewall issue. As for the URL, I copied it directly from the tnsnames.ora, so the same URL that PL/SQL Developer is using. – Alex Mar 24 '15 at 19:01
  • I have also tried to specify the IP address of the server rather than the domain name, which results in the same exception. – Alex Mar 24 '15 at 19:03
  • I could be wrong but for me it looks like you are specifying the username and pw twice. once as hr/hr (which i guess is from the sample application?) and than using user, password); what i would try is tom remove the hr/hr – Ueli Hofstetter Mar 24 '15 at 19:13
  • I tried removing the "hr/hr" portion, as well as changing the connection string as recommended by morgano below, still no success :/ – Alex Mar 24 '15 at 19:37
  • We use a similar connection string, but ours does not have the username and password in the url (assuming thats what your `hr/hr` is). We just have `jdbc:oracle:thin:@(DESCRIPTION...`. You might try that, since your username and password are already provided to the DriverManager in the method call. – cjstehno Mar 24 '15 at 19:59

2 Answers2

1

The connection string is of the correct format. The problem is that the host isn't set. It's null, or perhaps the string "null". There's simply no other way to generate the error message java.net.UnknownHostException: null

In your sample code, you write String host = "HOST_NAME";. This makes it look like in your real code you are assigning a constant string to the variable host. However, I'm going to stick my neck out and say that in your real code you are not doing this. You are looking up the host name from somewhere, this lookup fails for some reason, you don't check this, and hence you pass a null value into the connection string.

Luke Woodward
  • 56,377
  • 16
  • 76
  • 100
0

Your JDBC url is wrong, according to the documentation it should be something like:

jdbc:oracle:driver_type:[username/password]@//host_name:port_number/service_name

In your case your code would be something like:

import java.sql.*;

public class Main {

    public Main () {
        try {
            String host = "HOST_NAME";
            String port = "1521";
            String service = "SERVICE_NAME";
            String user = "SCHEMA_USER";
            String password = "SCHEMA_PASSWORD";

            Class.forName("oracle.jdbc.driver.OracleDriver");

            Connection connection = DriverManager.getConnection(
                "jdbc:oracle:thin:@//" + host
                + ":" + port + "/" + service, user, password);

            connection.close ();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main (String args) {
        new Main ();
    }
}
morgano
  • 16,553
  • 9
  • 42
  • 51
  • I tried specifying it just like in your example, but I still receive the same errors. My original connection string was formatted using the same format as in the tnsnames.ora file – Alex Mar 24 '15 at 19:36
  • The mane of the driver, try: `oracle.jdbc.OracleDriver` instead of `oracle.jdbc.driver.OracleDriver` – morgano Mar 24 '15 at 22:39