Java: JDBC: Connect with existing connection

If our java program runs in an already connected environment, it is not necessary to open a new connection with jdbc.

It is just enough to use jdbc:default:connection string. This is useful e.g. in a “JAVA OBJECT” of oracle database, where the connection already exists.

This kind of connection is called “Server Side JDBC” and it is not needed to change at every password change. Here is an example:

     public OracleConnection getOracleDataSourceConnection(){  
    
         OracleConnection tempOracleconnection=null;
        
         try {
             OracleDataSource ods = new OracleDataSource();
             ods.setURL("jdbc:default:connection");
             tempOracleconnection = (OracleConnection) ods.getConnection();
         }
         catch (SQLException e) {
             e.printStackTrace();
         }
        
        return tempOracleconnection;
     }

Just to remember the “Client Side JDBC” has the following format:

jdbc:oracle:thin:myUser/myPwd@myHost:1521:myDB

Leave a Reply