This week's book giveaway is in the JavaScript forum.
We're giving away four copies of Svelte and Sapper in Action and have Mark Volkmann on-line!
See this thread for details.
Win a copy of Svelte and Sapper in Action this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Simple Add using JNDI

 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
I installed OpenLDAP and i am trying to access slapd server using JNDI. Now, I am trying to add using JNDI. following is an example program taken from this mailing list.which I have modified according to my values.

Thanks
Prabhakaran.N

Add.java
--------

package test;

import java.util.Hashtable;
import java.util.*;
import javax.naming.*;
import javax.naming.directory.*;

class Add{

final static String rootContext = "dc=mycompany,dc=com";
final static String rootdn = "dc=Mobius,dc=mycompany,dc=com";

public static void main(String[] args) {

Hashtable env = new Hashtable(5, 0.75f);

env.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.ldap.LdapCtxFactory");

// host and port to use for directory service
env.put(Context.PROVIDER_URL, "ldap://localhost:389";);

// authentication information
env.put(Context.SECURITY_AUTHENTICATION, "simple");
env.put(Context.SECURITY_PRINCIPAL, "secret");
env.put(Context.SECURITY_CREDENTIALS, "secret");

// entry's DN
String entryDN = "cn=people, " + rootContext ;

// entry's attributes

Attribute cn = new BasicAttribute("cn", "NewUser");
Attribute sn = new BasicAttribute("sn", "Smith"); // mandatory attribute
Attribute mail = new BasicAttribute("mail", "newuser@foo.com");
Attribute phone = new BasicAttribute("telephoneNumber", "+1 222 333 4444");

Attribute oc = new BasicAttribute("objectClass");
oc.add("top");
oc.add("person");
oc.add("organizationalPerson");
oc.add("inetOrgPerson");


DirContext ctx = null;

try {
// get a handle to an Initial DirContext
ctx = new InitialDirContext(env);

// build the entry
Attributes entry = new BasicAttributes();
entry.put(cn);
entry.put(sn);
entry.put(mail);
entry.put(phone);
entry.put(oc);

// Add the entry
ctx.createSubcontext(entryDN, entry);
System.out.println( "AddUser: added entry " + entryDN + ".");

} catch (NamingException e) {
System.err.println("AddUser: error adding entry." + e);
}
}
}
 
Don't destroy the earth! That's where I keep all my stuff! Including this tiny ad:
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
    Bookmark Topic Watch Topic
  • New Topic