4 Replies Latest reply on Sep 4, 2008 9:17 AM by 387878

    How to use Oracle JDBC driver to access an LDAP server ?

    David Le Borgne
      Hello,

      I'm trying to use Oracle JDBC driver 10.1.0.5 to access my LDAP server. After reading
      http://download.oracle.com/docs/cd/B28359_01/java.111/b31224/urls.htm#CHDBICFA

      I wrote the following code:
      import oracle.jdbc.pool.OracleDataSource;
      import java.sql.Connection;
      import java.util.Properties;
      
      public class TestLdap {
      
        private static OracleDataSource ods;
        private static Connection cnx;
      
        public static void main (String args[]) {
      
          Properties prop = new Properties();
          String url = "jdbc:oracle:thin:@ldap://ldap-server:389/";
      
          try {
            ods = new OracleDataSource();
            ods.setURL(url);
            ods.setConnectionCachingEnabled(true);
            cnx = ods.getConnection("CN=Admin,O=COHERIS,C=FR", "password");
          } catch (java.sql.SQLException se) {
            se.printStackTrace();
          }
        }
      }
      It fails with java.sql.SQLException: Exception d'E/S: JNDI Package failurejavax.naming.InvalidNameException: cn=: [LDAP: error code 34 - Invalid DN Syntax]; remaining name 'cn='

      I have tried many different URLs, but couldn't get a working Connection.
      My LDAP server is Domino 6.5, but I did some testing with OpenLDAP 2.0 and had the same results.

      Does anyone has an example ? This Oracle JDBC driver feature seems quite uncommon, and I couldn't find any example on the Internet.

      Thanks
      David Le Borgne
        • 1. Re: How to use Oracle JDBC driver to access an LDAP server ?
          dvohra21
          Set the url, username and password as follows:
          String url = "jdbc:oracle:thin:@ldap://ldap-server:389/CN=Admin,O=COHERIS,C=FR";
          
          
          Properties prop = new Properties();
          prop.put("java.naming.security.authentication", "simple");
          prop.put("java.naming.security.principal","CN=Admin,O=COHERIS,C=FR");
          prop.put("java.naming.security.credentials", "password");
          
          OracleDataSource ods = new OracleDataSource();
          ods.setURL(url);
          ods.setConnectionProperties(prop);
          • 2. Re: How to use Oracle JDBC driver to access an LDAP server ?
            387878
            Hi,
            sorry for reopening this old thread but I'm stuck logging on an LDAP server.

            This is my ldap.ora:
            DIRECTORY_SERVERS=(server1:389:636,server2:389:636)
            DEFAULT_ADMIN_CONTEXT="dc=nl,dc=abnamro,dc=com"
            DIRECTORY_SERVER_TYPE=OID

            normally I simply do from sqlplus:
            sqlplus aldo/aldo@mts-a-de-grid

            may you please repost your code replacing my cutom values?
            I know my request is quite stupid but I can't understand were is the user and password values in your code...

            tanks a lot
            aldo
            • 3. Re: How to use Oracle JDBC driver to access an LDAP server ?
              friscoki
              Hello,

              Does your ldap setup allow anonymous binds or do you need to authenticate? Are you using wallets and NAMES.LDAP_AUTHENTICATE_BIND to allow sqlplus to connect?

              This code should connect to a database using ldap as the name resolution method with ldap anonymous binds. If you are using authenticated ldap, then you must add the additional code above to connect to ldap.
              String ldapHost    = "ldap-server:389";
              String context     = ",dc=nl,dc=abnamro,dc=com";
              
              String serviceName = "mts-a-de-grid";
              String user        = "aldo";
              String pass        = "aldo";
              String url         = "jdbc:oracle:thin:@ldap://" + ldapHost + "/" + instance + ldap_context;
              
              OracleDataSource o = new OracleDataSource();
              
              o.setUser(user);
              o.setPassword(password);
              o.setURL(url);
              
              Connection conn = o.getConnection();
              • 4. Re: How to use Oracle JDBC driver to access an LDAP server ?
                387878
                Tanks for post friscoki.
                Now I'm able to connect.

                Given your code, I only added "OracleContext" to the context since server type is OID

                thanks for help, bye
                aldo