与Java中连接MongoDB的问题

huangapple go评论120阅读模式
英文:

Problem with connecting to MongoDB on Java

问题

  1. 我在Java中使用MongoDB遇到了问题我无法连接到数据库总是收到异常并且无法解决我使用了Maven依赖来实现我尝试过使用其他版本的依赖
  2. 我还确认了MongoDB是否在运行我使用 *net start MongoDB* 命令启动了它
  3. 我的代码
  4. import com.mongodb.MongoClient;
  5. import com.mongodb.MongoClientURI;
  6. public class MongoDBTest {
  7. public static void main(String[] args) throws Exception {
  8. MongoClient mongoClient = new MongoClient(new MongoClientURI("mongodb://127.0.0.1:27017"));
  9. }
  10. }
  11. 我的依赖
  12. <dependency>
  13. <groupId>org.mongodb</groupId>
  14. <artifactId>mongo-java-driver</artifactId>
  15. <version>3.10.0</version>
  16. </dependency>
  17. 而我收到的异常信息
  18. Exception in thread "main" java.lang.NoSuchMethodError: com.mongodb.ConnectionString.getThreadsAllowedToBlockForConnectionMultiplier()Ljava/lang/Integer;
  19. at com.mongodb.MongoClientURI.getOptions(MongoClientURI.java:351)
  20. at com.mongodb.Mongo.createCluster(Mongo.java:724)
  21. at com.mongodb.Mongo.<init>(Mongo.java:312)
  22. at com.mongodb.Mongo.<init>(Mongo.java:308)
  23. at com.mongodb.MongoClient.<init>(MongoClient.java:326)
  24. at schlueting.arbeiten.MongoDBTest.main(MongoDBTest.java:9)
英文:

I have a problem with my MongoDB in Java. I cannot connect to it I always get an exception and could not resolve it. I use Maven Dependencies for that. I tried using other versions of the dependency.

I also checked that the MongoDB is running. I started it with net start MongoDB.
My code:

  1. import com.mongodb.MongoClient;
  2. import com.mongodb.MongoClientURI;
  3. public class MongoDBTest {
  4. public static void main(String[] args) throws Exception {
  5. MongoClient mongoClient = new MongoClient(new MongoClientURI(&quot;mongodb://127.0.0.1:27017&quot;));
  6. }
  7. }

My dependency:

  1. &lt;dependency&gt;
  2. &lt;groupId&gt;org.mongodb&lt;/groupId&gt;
  3. &lt;artifactId&gt;mongo-java-driver&lt;/artifactId&gt;
  4. &lt;version&gt;3.10.0&lt;/version&gt;
  5. &lt;/dependency&gt;

And the exception i got:

  1. Exception in thread &quot;main&quot; java.lang.NoSuchMethodError: com.mongodb.ConnectionString.getThreadsAllowedToBlockForConnectionMultiplier()Ljava/lang/Integer;
  2. at com.mongodb.MongoClientURI.getOptions(MongoClientURI.java:351)
  3. at com.mongodb.Mongo.createCluster(Mongo.java:724)
  4. at com.mongodb.Mongo.&lt;init&gt;(Mongo.java:312)
  5. at com.mongodb.Mongo.&lt;init&gt;(Mongo.java:308)
  6. at com.mongodb.MongoClient.&lt;init&gt;(MongoClient.java:326)
  7. at schlueting.arbeiten.MongoDBTest.main(MongoDBTest.java:9)

答案1

得分: 2

解决方案是将以下内容添加到Maven依赖项中:

  1. <dependency>
  2. <groupId>org.mongodb</groupId>
  3. <artifactId>mongodb-driver-sync</artifactId>
  4. <version>3.10.1</version>
  5. </dependency>

这解决了异常问题,我成功连接到了MavenDB。

顺便提一下,我当时使用的是MongoDB 4.4和JDK 11。

英文:

The solution was to add the following to the Maven dependencies:

  1. &lt;dependency&gt;
  2. &lt;groupId&gt;org.mongodb&lt;/groupId&gt;
  3. &lt;artifactId&gt;mongodb-driver-sync&lt;/artifactId&gt;
  4. &lt;version&gt;3.10.1&lt;/version&gt;
  5. &lt;/dependency&gt;

That fixed the Exception and I could connect to the MavenDB.

Btw. I was using MongoDB 4.4 and JDK 11.

huangapple
  • 本文由 发表于 2020年10月27日 17:57:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/64551976.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定