新手,往ldap插入記錄出錯。
插入的代碼:
package com.liang.ldap;
import java.util.Hashtable;
import javax.naming.Context;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;
public class JNDIAdd {
public static void main(String args[]) {
try {
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, "ldap://localhost:389/");
env.put(Context.SECURITY_AUTHENTICATION,"simple");
env.put(Context.SECURITY_PRINCIPAL,"cn=root,dc=liang,dc=com"); // specify the username
env.put(Context.SECURITY_CREDENTIALS,"liang"); // specify the password
DirContext ctx = new InitialDirContext(env);
MyObject obj = new MyObject();
ctx.bind("cn=user1,ou=admin,dc=liang,dc=com", obj);
System.out.println("OK");
} catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
}
}
對象的java 源文件:
package com.liang.ldap;
import java.io.Serializable;
public class MyObject implements Serializable {
}
出現錯誤:
javax.naming.directory.InvalidAttributeIdentifierException: ; remaining name 'cn=user1,ou=admin,dc=liang,dc=com'
at com.sun.jndi.ldap.LdapCtx.mapErrorCode(LdapCtx.java:3054)
at com.sun.jndi.ldap.LdapCtx.processReturnCode(LdapCtx.java:2931)
at com.sun.jndi.ldap.LdapCtx.processReturnCode(LdapCtx.java:2737)
at com.sun.jndi.ldap.LdapCtx.c_bind(LdapCtx.java:379)
at com.sun.jndi.ldap.LdapCtx.c_bind(LdapCtx.java:336)
at com.sun.jndi.toolkit.ctx.ComponentContext.p_bind(ComponentContext.java:596)
at com.sun.jndi.toolkit.ctx.PartialCompositeContext.bind(PartialCompositeContext.java:183)
at com.sun.jndi.toolkit.ctx.PartialCompositeContext.bind(PartialCompositeContext.java:173)
at javax.naming.InitialContext.bind(InitialContext.java:359)
at com.liang.ldap.JNDIAdd.main(JNDIAdd.java:22)
為什麼會出現這樣的錯誤呢?我應該如何做?
《解決方案》
你這個問題說起來就長了,我不知道你除了代碼個具體還做了什麼工作,我認為你要把java對象Serialized到目錄中,首先你要去加一個java.schema
這樣起碼你有相應的schema來存儲,
你果斷的就給出了 cn=user1,ou=developers,dc=dcme,dc=cn 證明你對目錄存儲本身並不了解。
另外你的出錯提示大約是:
在嘗試添加或修改不完全或不正確地指定的屬性集時,拋出此異常。例如,在嘗試添加或修改一個綁定,或者創建新的子上下文而未指定創建對象所需的全部強制屬性時,可能發生此情況。另一種拋出此異常的情況是,在同一個屬性集中指定了相互不兼容的屬性,或者指定的屬性與對象模式指定的屬性有衝突。
[ 本帖最後由 wangbaohua 於 2007-4-29 16:03 編輯 ]
《解決方案》
在配置文件中加入 java 的schema之後我的對象就可以存入資料庫了。多謝指點。後來看了 java 核心技術卷II第七版就明白了。