开发者社区> 问答> 正文

我正在尝试使用Eclipse IDE连接到mongodb,出现错误

错误是:

java.lang.NoClassDefFoundError: com/mongodb/MongoTimeoutException
        bean.Dao.getValue(Dao.java:18)
        bean.Login.doPost(Login.java:60)

*

包装豆; 导入java.net.UnknownHostException; 导入org.apache.log4j.Logger; 导入com.mongodb.BasicDBObject; 导入com.mongodb.DB; 导入com.mongodb.DBCollection; 导入com.mongodb.DBCursor; 导入com.mongodb.MongoClient; 导入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; 

    }

}

从该文件调用Examlpe.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);

   }

   }

展开
收起
几许相思几点泪 2019-12-23 17:56:19 1224 0
0 条回答
写回答
取消 提交回答
问答排行榜
最热
最新

相关电子书

更多
MongoDB多数据中心的方案选型之路 立即下载
D2前端工程下一站 IDE ——上坡&吭头 立即下载
D2前端工程下一站 IDE 立即下载

相关镜像