mongodb java 拒绝连接,无法使用Java连接到MongoDB

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.

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?

解决方案

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

I think it will be better solution to connect database.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用 Java 编程语言与 MongoDB 数据库建立连接。下面是一个简单的代码示例,演示如何连接MongoDB 并执行一些基本操作: ```java import com.mongodb.MongoClient; import com.mongodb.MongoClientURI; import com.mongodb.client.MongoDatabase; import org.bson.Document; public class MongoDBJavaConnection { public static void main(String[] args) { // MongoDB 连接字符串 String connectionString = "mongodb://localhost:27017"; // 创建 MongoDB 客户端 MongoClientURI uri = new MongoClientURI(connectionString); MongoClient mongoClient = new MongoClient(uri); // 连接到数据库 MongoDatabase database = mongoClient.getDatabase("your_database_name"); // 在集合中插入文档 Document document = new Document("name", "John Doe") .append("age", 30) .append("email", "johndoe@example.com"); database.getCollection("your_collection_name").insertOne(document); // 在集合中查询文档 Document query = new Document("name", "John Doe"); Document result = database.getCollection("your_collection_name").find(query).first(); System.out.println(result); // 关闭连接 mongoClient.close(); } } ``` 请确保你已经将 MongoDBJava 驱动程序添加到你的项目依赖中。上述示例代码使用的是 MongoDB Java 驱动程序的默认版本。 你需要将 `your_database_name` 替换为你要连接的实际数据库名称,将 `your_collection_name` 替换为你要执行操作的集合名称。然后,你可以根据需要修改代码以适应你的业务逻辑。 希望这可以帮助你开始使用 MongoDBJava 进行连接和操作!如有任何进一步问题,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值