My Oracle Support Banner

How to Connect to Both an 11g RDBMS and a 12c RDBMS With the Same JDBC Thin Code? (Doc ID 2216886.1)

Last updated on DECEMBER 28, 2016

Applies to:

JDBC - Version 12.1.0.2.0 and later
Information in this document applies to any platform.

Goal

How can you connect to both an 11g database and a 12c database with the same Java/JDBC code?

This can be accomplished if the hostname (or IP address), port number, and service name are provided as input parameters rather than hard-coded.
The syntax of the connect string is key.  For example, the following code snippet may throw an error:

public static Connection getConnection(String ip,String userName,String password, String instanceName, int port) {
   try {
      String dbUrl = "jdbc:oracle:thin:@"+ip+":"+port+":"+instanceName;
      Class.forName("oracle.jdbc.driver.OracleDriver");
      //oracle.jdbc.OracleTypes.TRACE = true;
      return DriverManager.getConnection(dbUrl, userName, password);
   } catch (Exception e) {
      log.error(e,e);
   return null;
   }
}

 

 

Solution

To view full details, sign in with your My Oracle Support account.

Don't have a My Oracle Support account? Click to get started!


In this Document
Goal
Solution
References


My Oracle Support provides customers with access to over a million knowledge articles and a vibrant support community of peers and Oracle experts.