使用UnboundID LDAP SDK API的Java问题(带有参数的调用方法)

时间:2018-11-21 06:39:57

标签: java ldap unboundid-ldap-sdk

我正在为我的LDAP服务器使用UnboundID LDAP SDK。 我提出了一种连接方法。

public static LDAPConnection connectSDK(String ip, Integer port, String id, String pw) throws LDAPException    {

    LDAPConnection ldap = new LDAPConnection(ip,port,id,pw);
    System.out.println("success");  
    return ldap;

}

在我的main方法中,我使用参数调用此方法,并且效果很好。

public static void main(String[] args) throws LDAPException {

    connectSDK("192.168.0.60",389,"******","*****");

}   

我想走得更远。我提出了一种使用过滤器搜索数据的方法。

public static void searchSDK(String filter) throws LDAPException {

    LDAPConnection ldap = connectSDK(); 

/* Before calling a method with parameter, I used to connect with this,
 and use 'ldap' variable to put search results. 
Now, I am using a method with parameters. 
I don't know what to do with 'ldap' variable. 
If I delete it, 'SearchRequest' doesn't run.  
Also how can I continue to use the connection from connectSDK method? */

    SearchRequest searchRequest = new SearchRequest("c=kr",SearchScope.SUB,filter);
    SearchResult searchResult = ldap.search(searchRequest);
    System.out.println(searchResult);

}

最终,我想像下面这样在我的主方法中调用其中两个方法。

public static void main(String[] args) throws LDAPException {
    //connect
    connectSDK("192.168.0.60",389,"*****","******");
    //search using a filter 
    searchSDK("hotdog");
}  

我想在我的searchSDK()方法中提出建议。
 * 1)如何使用创建的会话connectSDK方法
 * 2)如何处理“ ldap”变量。
 * 3)ldap.close()我想在之后关闭会话,但是该方法不起作用。还有其他办法吗?

1 个答案:

答案 0 :(得分:1)

您的代码LDAPConnection ldap = connectSDK();中没有这样的方法,因为connectSDK(String ip, Integer port, String id, String pw)返回一个LDAPConnection,然后将此作为参数传递给searchSDK并从其中删除connectSDK()

将代码修改为

public static void searchSDK(String filter, LDAPConnection ldap) throws LDAPException {

    // LDAPConnection ldap = connectSDK(); 
....
}

所以您的主体看起来像

LDAPConnection ldap = connectSDK("192.168.0.60",389,"*****","******");
//search using a filter 
searchSDK("hotdog", ldap);