用JLDAP,我倒是做过,
查询的例子
import com.novell.ldap.*;
import com.novell.ldap.util.Base64;import java.util.Enumeration;
import java.util.Iterator;
import java.util.Random;public class LDAPSearch {
private static String[] querys = new String[]{
"id=456",
"gender=0",
"qq=4566642",
"mobile=ef",
"icq=9032454",
"province=hunan",
"age=4938345",
"country=china",
"phone=5ff-a67",
"state=1",
"[email protected]",
"address=4e3"
};
private static Random random = new Random(); public static String randomQuery(){
return querys[random.nextInt(querys.length)];
} public static void main(String[] args) {
int searchScope = LDAPConnection.SCOPE_ONE;
String searchBase = "ou=UserInfo,dc=konlink,dc=com";
String searchFilter = randomQuery();
System.out.println(searchFilter);
try {
LDAPConnection lc = getConnection(); long start = System.currentTimeMillis();
LDAPSearchResults searchResults =
lc.search(searchBase,
searchScope,
searchFilter,
null, // return all attributes
false); // return attrs and values System.out.println("query cost time: " + (System.currentTimeMillis() - start));
/* To print out the search results,
* -- The first while loop goes through all the entries
* -- The second while loop goes through all the attributes
* -- The third while loop goes through all the attribute values
*/
while (searchResults.hasMore()) {
LDAPEntry nextEntry = null;
try {
nextEntry = searchResults.next();
}
catch (LDAPException e) {
System.out.println("Error: " + e.toString()); // Exception is thrown, go for next entry continue;
} System.out.println("\n" + nextEntry.getDN());
System.out.println(" Attributes: "); LDAPAttributeSet attributeSet = nextEntry.getAttributeSet();
Iterator allAttributes = attributeSet.iterator(); while (allAttributes.hasNext()) {
LDAPAttribute attribute =
(LDAPAttribute) allAttributes.next();
String attributeName = attribute.getName(); System.out.println(" " + attributeName); Enumeration allValues = attribute.getStringValues(); if (allValues != null) {
while (allValues.hasMoreElements()) {
String Value = (String) allValues.nextElement();
if (Base64.isLDIFSafe(Value)) {
// is printable
System.out.println(" " + Value);
}
else {
// base64 encode and then print out
Value = Base64.encode(Value.getBytes());
System.out.println(" " + Value);
}
}
}
}
}
// disconnect with the server
dispose(lc);
}
catch (LDAPException e) {
System.out.println("Error: " + e.toString());
}
/*catch (UnsupportedEncodingException e) {
System.out.println("Error: " + e.toString());
} */
System.exit(0);
} public static LDAPConnection getConnection() {
int ldapPort = LDAPConnection.DEFAULT_PORT;
int ldapVersion = LDAPConnection.LDAP_V3;
String ldapHost = "localhost";
String loginDN = "cn=root,dc=konlink,dc=com";
String password = "111111";
LDAPConnection lc = new LDAPConnection();
try {
// connect to the server
lc.connect(ldapHost, ldapPort);
// bind to the server
lc.bind(ldapVersion, loginDN, password.getBytes("UTF8"));
}
catch(Exception e){
e.printStackTrace();
return null;
}
return lc; } public static void dispose(LDAPConnection lc){
try{
if(lc.isConnected()){
lc.disconnect();
}
}
catch(Exception e){
e.printStackTrace();
}
}
}
查询的例子
import com.novell.ldap.*;
import com.novell.ldap.util.Base64;import java.util.Enumeration;
import java.util.Iterator;
import java.util.Random;public class LDAPSearch {
private static String[] querys = new String[]{
"id=456",
"gender=0",
"qq=4566642",
"mobile=ef",
"icq=9032454",
"province=hunan",
"age=4938345",
"country=china",
"phone=5ff-a67",
"state=1",
"[email protected]",
"address=4e3"
};
private static Random random = new Random(); public static String randomQuery(){
return querys[random.nextInt(querys.length)];
} public static void main(String[] args) {
int searchScope = LDAPConnection.SCOPE_ONE;
String searchBase = "ou=UserInfo,dc=konlink,dc=com";
String searchFilter = randomQuery();
System.out.println(searchFilter);
try {
LDAPConnection lc = getConnection(); long start = System.currentTimeMillis();
LDAPSearchResults searchResults =
lc.search(searchBase,
searchScope,
searchFilter,
null, // return all attributes
false); // return attrs and values System.out.println("query cost time: " + (System.currentTimeMillis() - start));
/* To print out the search results,
* -- The first while loop goes through all the entries
* -- The second while loop goes through all the attributes
* -- The third while loop goes through all the attribute values
*/
while (searchResults.hasMore()) {
LDAPEntry nextEntry = null;
try {
nextEntry = searchResults.next();
}
catch (LDAPException e) {
System.out.println("Error: " + e.toString()); // Exception is thrown, go for next entry continue;
} System.out.println("\n" + nextEntry.getDN());
System.out.println(" Attributes: "); LDAPAttributeSet attributeSet = nextEntry.getAttributeSet();
Iterator allAttributes = attributeSet.iterator(); while (allAttributes.hasNext()) {
LDAPAttribute attribute =
(LDAPAttribute) allAttributes.next();
String attributeName = attribute.getName(); System.out.println(" " + attributeName); Enumeration allValues = attribute.getStringValues(); if (allValues != null) {
while (allValues.hasMoreElements()) {
String Value = (String) allValues.nextElement();
if (Base64.isLDIFSafe(Value)) {
// is printable
System.out.println(" " + Value);
}
else {
// base64 encode and then print out
Value = Base64.encode(Value.getBytes());
System.out.println(" " + Value);
}
}
}
}
}
// disconnect with the server
dispose(lc);
}
catch (LDAPException e) {
System.out.println("Error: " + e.toString());
}
/*catch (UnsupportedEncodingException e) {
System.out.println("Error: " + e.toString());
} */
System.exit(0);
} public static LDAPConnection getConnection() {
int ldapPort = LDAPConnection.DEFAULT_PORT;
int ldapVersion = LDAPConnection.LDAP_V3;
String ldapHost = "localhost";
String loginDN = "cn=root,dc=konlink,dc=com";
String password = "111111";
LDAPConnection lc = new LDAPConnection();
try {
// connect to the server
lc.connect(ldapHost, ldapPort);
// bind to the server
lc.bind(ldapVersion, loginDN, password.getBytes("UTF8"));
}
catch(Exception e){
e.printStackTrace();
return null;
}
return lc; } public static void dispose(LDAPConnection lc){
try{
if(lc.isConnected()){
lc.disconnect();
}
}
catch(Exception e){
e.printStackTrace();
}
}
}
解决方案 »
- 别人给我一个SSH项目 我该怎么运行起来
- 很无语的问题,别人用GOOGLE点进我的网站,,然后就出不去了。。只点X了。。点后退无效。。求解决办法。
- getServlet().getServletConfig().getServletContext().getRealPath("/")=NULL?
- struts2 标签从action取值问题
- 郁闷呀jsp+mysql问题
- 菜鸟提问:如何把这个程序变成webservice
- 在查找元素的时候 map,list ,set 相比那个速度快
- 请教一个小问题?
- 高分幫忙看一看 \ 轉換成 /的問題 急急急 在線等
- JAVA录音程序调用
- stuts 与servlet性能问题疑惑.
- 你一先后顺序.说一说
import com.novell.ldap.*;
import dbtest.entity.User;
import dbtest.util.UserFactory;import java.io.UnsupportedEncodingException;public class LDAPAdd {
public static void main(String[] args) {
int ldapPort = LDAPConnection.DEFAULT_PORT;
int ldapVersion = LDAPConnection.LDAP_V3;
String ldapHost = "localhost";
String loginDN = "cn=root,dc=konlink,dc=com";
String password = "111111";
String containerName = "ou=UserInfo,dc=konlink,dc=com";
LDAPConnection lc = new LDAPConnection(); try {
// connect to the server
lc.connect(ldapHost, ldapPort);
// authenticate to the server
lc.bind(ldapVersion, loginDN, password.getBytes("UTF8")); //long start = System.currentTimeMillis();
for (int i = 501; i < 1000; i++) {
for (int j = 0; j < 1000; j++) {
lc.add(getEntry(i * 1000 + j, containerName));
}
System.out.println("complete" + (i+1) + "k");
} //System.out.println(System.currentTimeMillis() - start);
//lc.add( newEntry );
//System.out.println( "\nAdded object: " + dn + " successfully." ); // disconnect with the server
lc.disconnect();
}
catch (LDAPException e) {
System.out.println("Error: " + e.toString());
}
catch (UnsupportedEncodingException e) {
System.out.println("Error: " + e.toString());
}
System.exit(0);
} private static LDAPEntry getEntry(int id, String containerName) {
这里代码可以自己写
}