无法使用Java连接到MongoDB

无法使用Java连接到MongoDB

本文介绍了无法使用Java连接到MongoDB的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个将连接到MongoDB数据库的Java程序。我已经拥有了我正在尝试访问的数据库的IP地址,端口,用户数据库,用户名和密码。

I'm trying to write a Java program that will connect to a MongoDB database. I already have the IP address, port, userDB, username, and password of the database I'm trying to access.

我将mongodb-driver-3.0.0.jar文件设置为Eclipse中的引用库。我在src文件夹中创建了一个MongoDBDriver.java文件(如下所示):

I have the mongodb-driver-3.0.0.jar file set as a referenced library in Eclipse. I created a MongoDBDriver.java file in the src folder (shown below):

import com.mongodb.MongoClient;

public class MongoDBDriver {

public static void main(String[] args) {

    MongoClient client = new MongoClient("10.66.80.152", 27017);
    String connectPoint = client.getConnectPoint();
    System.out.println(connectPoint);
    client.close();
  }

}

当我运行脚本时,我得到以下错误消息:

When I run the script, I get the following error message:

Exception in thread "main" java.lang.NoClassDefFoundError:
com/mongodb/connection/BufferProvider
at MongoDBDriver.main(MongoDBDriver.java:7)
Caused by: java.lang.ClassNotFoundException:
com.mongodb.connection.BufferProvider
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 1 more

那么,我是什么我做错了,如何解决?

So, what am I doing wrong and how can I fix it?

推荐答案

异常意味着您的类路径不匹配。
我创建连接java和mongodb的小应用程序并进行一些CURD操作。

Exception means that you have the class path mismatch.I create small app that connect java and mongodb and do some CURD operations.

eMongoDB-Java-app
我认为连接数据库将是更好的解决方案。

eMongoDB-Java-appI think it will be better solution to connect database.

这篇关于无法使用Java连接到MongoDB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-26 14:36