Hi,
Ich habe ein klein Programm geschrieben was Daten von ein Excel Tabelle Daten nehmen soll und die in ein LDAP Datenbank einträgt. Die Verbindung funktioniert nur weiß ich nicht warum die Eintragung nicht erfolgt. Hier unten mein Java Code und das Output nachdem das Programm ausgeführt ist.
Hier die Ausgabe:
Das Excel Document enthält diese Infos:
fdai0020 Buehler Mark AI
fdai0021 Wagner Toma AI
Ich habe ein klein Programm geschrieben was Daten von ein Excel Tabelle Daten nehmen soll und die in ein LDAP Datenbank einträgt. Die Verbindung funktioniert nur weiß ich nicht warum die Eintragung nicht erfolgt. Hier unten mein Java Code und das Output nachdem das Programm ausgeführt ist.
Java:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package hsappldap;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Iterator;
import javax.naming.Context;
import javax.naming.directory.Attribute;
import javax.naming.directory.BasicAttribute;
import javax.naming.directory.BasicAttributes;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
/**
*
* @author laurent
*/
public class HsAppLDAP {
/**
* @param args the command line arguments
*/
@SuppressWarnings("rawtypes")
public static void main(String[] args) {
// Déclaration des variables
@SuppressWarnings("Convert2Diamond")
ArrayList<String> values = new ArrayList<String>();
Connection connection;
Statement statement;
// Récupération des données depuis le fichier excel
try {
InputStream input = new FileInputStream("student.xls");
POIFSFileSystem fs = new POIFSFileSystem(input);
HSSFWorkbook wb = new HSSFWorkbook(fs);
HSSFSheet sheet = wb.getSheetAt(0);
Iterator rows = sheet.rowIterator();
while (rows.hasNext()) {
values.clear();
HSSFRow row = (HSSFRow) rows.next();
Iterator cells = row.cellIterator();
while (cells.hasNext()) {
HSSFCell cell = (HSSFCell) cells.next();
if (HSSFCell.CELL_TYPE_NUMERIC == cell.getCellType())
values.add(Integer.toString((int) cell.getNumericCellValue()));
else if (HSSFCell.CELL_TYPE_STRING == cell.getCellType())
values.add(cell.getStringCellValue());
}
// Insertion en BDD
String url = "ldap://10.10.10.10:389";
@SuppressWarnings("UseOfObsoleteCollectionType")
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, url);
env.put(Context.SECURITY_AUTHENTICATION, "none");
env.put(Context.SECURITY_PRINCIPAL, "uid=admin,o=organisation");
env.put(Context.SECURITY_CREDENTIALS, "Password");
try {
DirContext ctx = new InitialDirContext(env);
System.out.println("connected");
System.out.println(ctx.getEnvironment());
try {
Attribute cn = new BasicAttribute("cn", values.get(0));
System.out.println(cn);
Attribute sn = new BasicAttribute("sn", values.get(1));
System.out.println(sn);
Attribute fullName = new BasicAttribute("fullName", values.get(2) +" "+ values.get(1));
System.out.println(fullName);
Attribute givenName = new BasicAttribute("givenName", values.get(2));
System.out.println(givenName);
Attribute uid = new BasicAttribute("uid", values.get(0));
System.out.println(uid);
Attribute ou = new BasicAttribute("ou", values.get(3));
System.out.println(ou);
Attribute oc = new BasicAttribute("objectClass");
System.out.println(oc);
oc.add("Top");
oc.add("Person");
oc.add("organizationalPerson");
oc.add("inetOrgPerson");
BasicAttributes entry = new BasicAttributes();
entry.put(cn);
entry.put(sn);
entry.put(fullName);
entry.put(givenName);
entry.put(uid);
entry.put(ou);
entry.put(oc);
String entryDN = "cn=" + values.get(0) + ",ou=AI,o=organisation";
System.out.println("1 Etape");
//To Do Tommorrow
ctx.createSubcontext(entryDN, entry);
System.out.println("Enter");
} catch (Exception e) {
System.out.println(e.getMessage());
}
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
} catch (IOException e) {
}
}
}
Hier die Ausgabe:
Java:
run:
10.10.10.10:389
connected
{java.naming.provider.url=ldap://10.10.10.10:389, java.naming.factory.initial=com.sun.jndi.ldap.LdapCtxFactory, java.naming.security.principal=uid=admin,o=organisation, java.naming.security.authentication=none, java.naming.security.credentials=Password!}
cn: fdai0021
sn: Wagner
fullName: Toma Wagner
givenName: Toma
uid: fdai0021
ou: AI
objectClass: No values
1 Etape
[LDAP: error code 50 - NDS error: no access (-672)]
BUILD SUCCESSFUL (total time: 22 seconds)
Das Excel Document enthält diese Infos:
fdai0020 Buehler Mark AI
fdai0021 Wagner Toma AI