Я пытаюсь подключиться к mongodb, используя Eclipse IDE, получая следующую ошибку:
java.lang.NoClassDefFoundError: com/mongodb/MongoTimeoutException
bean.Dao.getValue(Dao.java:18)
bean.Login.doPost(Login.java:60)
Мой код:
package bean;
import java.net.UnknownHostException;
import org.apache.log4j.Logger;
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.MongoClient;
import com.mongodb.MongoTimeoutException;
public class DBConnection1 {
static MongoClient mongoClient ;
private static final Logger logger=Logger.getLogger(DBConnection1.class);
public static DB getCollection(){
// TODO Auto-generated method stub
try{
mongoClient = new MongoClient( "gpd-56b-bc56.nam.nsroot.net", 27017 );
DB db = mongoClient.getDB( "emportal" );
//LOGGER.info("Connect to database successfully");
String username="emPortalAdmin";
char[] password={'e','m','P','o','r','t','a','l','A','d','m','i','n','1','2','3'};
@SuppressWarnings("deprecation")
boolean auth = db.authenticate(username,password);
//LOGGER.info(auth);
return db;
}
catch(MongoTimeoutException|UnknownHostException e) {
System.out.println(e.getMessage());
e.printStackTrace();
}
return null;
}
}
Файл DAo.java
package bean;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
public class Dao {
public static String getValue()throws Exception {
DBCursor cursor=null;
String env=null;
DB db=DBConnection1.getCollection();
DBCollection coll=db.getCollection("Employee");
List<String> envlist=new ArrayList<String>();
cursor=coll.find();
while(cursor.hasNext())
{
DBObject obj = cursor.next();
env=(String)obj.get("Employeeid");
if(!env.equalsIgnoreCase("NA")){
envlist.add(env);
}
}
cursor.close();
//// DBConnectionUtil.mongoClient.close();
Collections.sort(envlist);
return env;
}
}
Example.java, вызывающий из этого файла:
package bean;
public class Example {
public static void main(String[] args) throws Exception { // TODO Auto-generated method stub
Dao k = new Dao();
String dfd=Dao.getValue();
System.out.println("sdsd"+dfd);
}
}