Oracle 12c database connection using thin driver throws IO error

corporateWhore picture corporateWhore · Apr 15, 2015 · Viewed 18.2k times · Source

I'm following the JDBC Developer's Guide and trying to test the JDBC thin driver connection using a short java program.

import java.sql.*;
import oracle.jdbc.*;
import oracle.jdbc.pool.OracleDataSource;
class JDBCVersion
{
public static void main (String args[]) throws SQLException
{
        OracleDataSource ods = new OracleDataSource();
        ods.setURL("jdbc:oracle:thin:hr/hr@localhost:1522:orcl");
        Connection conn = ods.getConnection();
        // Create Oracle DatabaseMetaData object
        DatabaseMetaData meta = conn.getMetaData();
        // gets driver info:
        System.out.println("JDBC driver version is " + meta.getDriverVersion());
}
} //<host>:<port>:<service>

I've tried every possible <host>:<port>:<service> combination but still get a java.sql.SQLRecoverableException: IO Error: The Network Adapter could not establish the connection

I've successfully tested the OCI driver using another program included in the tutorial....but can't get this one to work. My application will be using the thin driver to connect to the database so my frustration level is....climbing.

Any help is appreciated.

Answer

SubOptimal picture SubOptimal · Apr 15, 2015

Maybe following comments could explain why you need the sevice name instead of the SID in the URL.

  • the Oracle JDBC FAQ mention that SIDs will be cease to be supported in one of the next few releases of the database

  • the Oracle JDBC devolopers guide mention Always connect to a service. Never use instance_name or SID because these do not direct to known good instances and SID is deprecated

  • the Oracle 2 day + Java developer tutorial mention the syntax jdbc:oracle:driver_type:[username/password]@//host_name:port_number:SID which seems to be a mixture of SID and service name URL (following the other documents and your working example)

  • in contrast the javadoc for OracleDriver mention only the SID syntax

  • the Oracle FAQ wiki mention both syntax

.

jdbc:oracle:thin:[USER/PASSWORD]@[HOST][:PORT]:SID
jdbc:oracle:thin:[USER/PASSWORD]@//[HOST][:PORT]/SERVICE