简体   繁体   中英

How can I make a Java app that connects to a MongoDB Atlas Cluster?

I'm trying to make a Java app that connects to a MongoDB Atlas cluster. I'm currently using Eclipse as my IDE.

From what I've read I'm supposed to use a Maven project to install dependencies, but since I prefer downloading JAR files and using them in my project I've downloaded 3 files (which are the ones that Maven downloads when you add the dependency for MongoDB): mongodb-driver-sync-4.1.1 ; mongodb-drive-core-4.1.1 and bson-4.1.1 from https://repo1.maven.org/maven2/org/mongodb/ and then added those files to my Java project. Using this method things like MongoDatabase and MongoClient work fine, but the problem is that whenever I try to use MongoCollection <Document> name = database.getCollection("collection") I get the error "Document cannot be resolved to a type", as if the IDE wouldn't recognize it. It's also worth noting that when trying to use MongoClientURI I get the same error.

在此处输入图像描述

I've tried creating a Maven project with the dependency mentioned before and it works just fine, I don't get errors from Document nor MongoClientURI, but when I create a class in the "main" folder that Maven creates and try to execute it I get a message error saying "launch error editor does not contain a main type", which of course, the class contains.

在此处输入图像描述

If I try to move the class to src/main/java (which by the way it's where it's first created) I get the same error as when using the Java project

在此处输入图像描述

(Note that MongoDB is the Java project and Mongo is the Maven project)

Guess I'm doing something wrong, so I guess I'll stick with a Java project (also given the fact that I want to use Window Builder and I can't seem to make it work with Maven).

I know it's a lot, but could you guys point me in the right direction? Thanks in advance.

Okay, apparently I wasn't importing org.bson.* , which contains the Document class... So that's solved

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM