简体   繁体   English

将 java 项目连接到 mongodb 数据库

[英]Connect java project to mongodb database

I'm trying to connect java project to my mongodb database.我正在尝试将 java 项目连接到我的 mongodb 数据库。 But I keep recieving this error although I imported the mongodb driver to the project:但是,尽管我将 mongodb 驱动程序导入到项目中,但我仍然收到此错误:

Exception in thread "main" java.lang.NoClassDefFoundError: com/mongodb/internal/connection/ServerAddressHelper线程“主”java.lang.NoClassDefFoundError 中的异常:com/mongodb/internal/connection/ServerAddressHelper

and that's my connection code:这就是我的连接代码:

MongoClient mongoClient = new MongoClient(new 
MongoClientURI("mongodb://localhost:27017"));
MongoDatabase database = mongoClient.getDatabase("Etudiant");
MongoCollection collection = database.getCollection("EtudiantC");
System.out.println("connected!");

The NoClassDefFoundError exception tells you that the class was there when the code you run was compiled, but it is missing in your application's classpath now. NoClassDefFoundError异常告诉您,当您运行的代码被编译时,class 就在那里,但现在您的应用程序的类路径中缺少它。

The most probable explanation is that you did add the mongodb-driver.jar to your classpath, but forgot about adding its transitive dependencies as well.最可能的解释是您确实将mongodb-driver.jar添加到您的类路径中,但也忘记了添加其传递依赖项。 The reported missing class ServerAddressHelper is present inside the mongodb-driver-core.jar .报告的缺失 class ServerAddressHelper存在于mongodb-driver-core.jar中。

So how to solve this problem?那么如何解决这个问题呢? Either use a dependency management system like Maven or Gradle for automatically downloading all the necessary jar-s, or you need to do this by other means (eg manually).要么使用 Maven 或 Gradle 之类的依赖管理系统来自动下载所有必要的 jar-s,要么您需要通过其他方式(例如手动)执行此操作。 It seems like you may also use the all-in-one mongo-java-driver.jar instead - see project's official documentation for details (search for "Binaries" on the page).看来您也可以改用一体机 mongo-java-driver.jar - 有关详细信息,请参阅项目的官方文档(在页面上搜索“二进制文件”)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在同一项目中连接两个数据库MySQL和MongoDB? 可能吗? - How to connect Two Database MySQL and MongoDB in the same project? Is it possible? 无法从 mongodb java 驱动程序连接到 MLab Mongo 数据库 - Can not Connect to MLab Mongo Database from mongodb java driver 如何在不使用URL指定数据库名称的情况下以Java连接到MongoDB? - How to connect to MongoDB in Java without specifying database name in the URL? 我正在使用jar文件连接mongodb和java的项目使用jsp和mongodb - i am using jsp and mongodb for my project using jar files to connect mongodb and java 无法通过Java项目连接到数据库,但可以使用客户端 - Can't connect to the database through java project but can using client 无法连接数据库到 Java 动态 Web 项目 - Can't connect database to Java Dynamic Web Project 谁能说出如何将Java项目连接到XAMP数据库? - Can anyone say how to connect a java project to a XAMP database? 如何连接到Android中的MongoDB数据库? - How to connect to a MongoDB database, in Android? 在 Spring-Boot 中,我们如何连接到同一个项目中的两个数据库(Mysql 数据库和 MongoDB)? - In Spring-Boot, How can we connect to two databases (Mysql database and MongoDB) in the same project? Java 用 mongodb 数据库测试 - Java test with mongodb database
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM